If I have a base class with a virtual destructor and derive another class from it with it's own destructor, will the code I put in the base class' destructor also be called when I destroy the derived object?
Also, if both the base class' default constructor and destructor are called when making/deleting an instance of a derived class, and these constructor and destructor call a protected member function of another class, do the derived classes have to be included as friends in that third class as well, or just the base class?
i.e. will the following work?
Would this call ListClassObject.RegisterObject(this); and ListClassObject.UnregisterObject(number);, where 'this' is what will be BaseClassPtr, and 'number' is the return value of the former, like I'd expect it to?Code:class BaseClass { public: BaseClass(); virtual ~BaseClass(); ~other members~ private int number; }; class DerivedClass: public BaseClass { public: DerivedClass(); virtual ~DerivedClass(); ~other members~ }; class ListClass { public: friend class BaseClass; protected: int RegisterObject(BaseClass *objecttoregister); void UnregisterObject(int number); }; ListClass ListClassObject; BaseClass::BaseClass() : number(ListClassObject.RegisterObject(this)) { // can I even use 'this' here? } BaseClass::~BaseClass() { ListClassObject.UnregisterObject(number); } DerivedClass::DerivedClass() { //initialise DerivedClass' new variables } DerivedClass::~DerivedClass() { // clean up DerivedClass' variables } int main() { BaseClass *BaseClassPtr=new DerivedClass; // Do stuff delete BaseClassPtr; return 0; }