I use Borland C++ Builder.
And i had o problem
#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
{
}
And for that new destructor that will inherite ~TObject
?
__fastcall TMyObject::~TMyObject?????????????
If you destroy a
TMyObject
through a reference of typeTMyObject
you don't have to do anything. In case you have a pointer/reference of typeTObject
to aTMyObject
things will go wrong. Only theTObject
destructor will be called, not theTMyObject
one:To have the decision on what destructor to call deferred to runtime, you need to specify the destructor as
virtual
inTObject
. Whenever you have a class that is intended to be derived from, the destructor should be virtual. Otherwise there is always a risk for resource leaks when the derived class destructor isn't called properly.What's causing the confusion to you is that you can specifically mention "which" constructor of the base class you want to use as in the following example. But you can't/ don't need to specify the destructor.
You could use a different constructor, say
TObject (int i)
by writingAn object can be destructed in only one way but it can be constructed in several ways (by having different constructors).
So, in short, you don't need to mention the name of the base class destructor in the derived class destructor. As soon as you destroy the derived object (say, by doing
delete derivedObj
), it will first call the derived class destructor and then base class destructor by itself.This can be solved at
TObject
's level. Its destructor has to be virtual:This way you can either do:
or
Both destructors will be called (
~TMyObject()
first and then~TObject()
) and you will have no leak.Destructor of the Base class will be automatically called by the compiler, when your objet life time ends. you do not need to call it explicitly.
Does not inherit the constructor.
It is called as Member initializer list and it initializes the Base class object with a particular value.
When you create the object.
The constructors will be called in order:
When the object life time ends the destructors will be called in the order:
The compiler will do so for you, do not need to call it explicitly.