如果我有一个带有虚拟析构函数的基类.有一个派生类来声明一个虚拟析构函数吗?
class base {
public:
virtual ~base () {}
};
class derived : base {
public:
virtual ~derived () {} // 1)
~derived () {} // 2)
};
Run Code Online (Sandbox Code Playgroud)
具体问题:
#include <iostream>
using namespace std;
class Car
{
public:
~Car() { cout << "Car is destructed." << endl; }
};
class Taxi :public Car
{
public:
~Taxi() {cout << "Taxi is destructed." << endl; }
};
void test(Car c) {}
int main()
{
Taxi taxi;
test(taxi);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
Car is destructed.
Car is destructed.
Taxi is destructed.
Car is destructed.
Run Code Online (Sandbox Code Playgroud)
我使用的是MS Visual Studio Community 2017(对不起,我不知道如何查看Visual C ++版本)。当我使用调试模式时。我发现在void test(Car c){ }按预期方式离开函数主体时执行了一个析构函数。而当一个额外的析构函数出现test(taxi);结束了。
该 …
从C++ FAQ:
[11.4]我可以为我的班级重载析构函数吗?没有.
我意识到这意味着你不能改变返回类型,参数的类型和参数的数量.我可能会分析单词的语法,但是可以覆盖 Parent的析构函数吗?
class Child : public Parent {
public:
virtual Parent::~Parent() {
// New definition
}
};
Run Code Online (Sandbox Code Playgroud)
那个问题是递归吗?
class Grandchild : public Child {
public:
Child::Parent::~Parent() {
// An even newer definition
}
};
Run Code Online (Sandbox Code Playgroud)
我已经阅读了这个和相关的帖子,它让我思考,因为析构函数不是继承的,它们不能被覆盖,但我从未见过它明确陈述过.
编辑:我改变了这一点,以反映我想覆盖Parent的析构函数,注意Child和Grandchild重写~Parent().
我这样做的主要原因是在改变它的销毁方式时保持Parent的界面(子类的完整原因).我将拥有管理所有Parent的其他内容,并将在我选择的稍后时间显式调用它们的析构函数.
我相信,Constructors和Destructors在base class不能被继承derived classes的基类的.我的理解是否正确.
我使用Borland C++ Builder.
我有问题
#include <Classes.hpp>
class TMyObject : public TObject
{
__fastcall TMyObject();
__fastcall ~TMyObject();//I would like to inherite my destructor from TObject
};
__fastcall TMyObject::TMyObject() : TObject()//it will inherited my constructor from TObject
{
}
Run Code Online (Sandbox Code Playgroud)
对于那个将继承的新析构函数~TObject?
__fastcall TMyObject::~TMyObject?????????????
Run Code Online (Sandbox Code Playgroud) 我在我的代码中发现了一个内存泄漏,它是由只调用对象的基类析构函数引起的。这个问题明白了:我已经virtual在接口类的析构函数中添加了MyÌnterface。让我感到困惑的是,编译器显然为我的助手类创建了一个标准的析构函数,MyHelper最终会被调用。我用两个不同的编译器试过这个。
这让我感到非常惊讶,因为我观察到如果成员或基类引入限制,则不会创建大多数默认实现。为什么析构函数的保护不被继承?
#include <iostream>
class MyInterface
{
public:
virtual void doSomethingUseful()=0;
// a lot more functions declared omitted
virtual void doSomethingElse()=0;
virtual void doSomethingIndividual()=0;
protected:
/// protected destructor to forbid calling it on interfaces
~MyInterface() {} // HERE the virtual is clearly missing
};
/// a common base that defaults most functions implementations
class MyHelper: public MyInterface
{
public:
void doSomethingUseful() {}
// a lot more default implementations omitted
void doSomethingElse() {}
}; …Run Code Online (Sandbox Code Playgroud) 我有一个类,Tile它有一个带有Color对象参数的构造函数:
class Tile
{
public:
static const int size = 32;
Tile();
Tile(Color &color);
void render(int x, int y);
Color getColor();
~Tile();
private:
Color _color;
};
Run Code Online (Sandbox Code Playgroud)
然后我有一个子类,TileGrass继承Tile:
class TileGrass : public Tile
{
public:
TileGrass();
~TileGrass();
};
Run Code Online (Sandbox Code Playgroud)
现在的问题是TileGrass需要Tile使用Color参数继承构造函数.但是,TileGrass对象已经知道它需要给超类提供什么颜色,所以我不想Color在创建新TileGrass对象时传入对象.在Java中,我可以这样做:
public class TileGrass extends Tile {
public TileGrass() {
super(color object);
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在C++中做这样的事情?
c++ ×7
inheritance ×6
destructor ×4
base-class ×1
c++builder ×1
constructor ×1
overriding ×1
visibility ×1