我使用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
{
}
对于将继承
~TObject
的新析构函数?__fastcall TMyObject::~TMyObject?????????????
最佳答案
这可以在TObject
的级别上解决。它的析构函数必须是虚拟的:
#include <Classes.hpp>
class TObject
{
__fastcall TObject();
virtual __fastcall ~TObject();
};
这样,您可以执行以下操作:
TObject * pobj = new TMyObject();
delete pobj;
或者
TMyObject * pobj = new TMyObject();
delete pobj;
两个析构函数都将被调用(首先是
~TMyObject()
,然后是~TObject()
),并且不会泄漏。关于c++ - C++中的析构函数和继承?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6856488/