Skirmish said:
I am deriving B from A. Is it possible to have both A and B's
destructor called by calling Release in the base object?
#include <stdio.h>
//-------------Base Object-------------//
class A
{
public:
A(void) {}
~A(void) {printf("A is deleted\n");}
void Release(void) {delete this;}
};
//-------------------------------------//
//------------Child Object-------------//
class B : public A
{
public:
B(void) {}
~B(void) {printf("B is deleted\n");}
};
//-------------------------------------//
void main(int argc, char **argv)
{
B *b = new B();
b->Release();
getchar(); //keep console open
}
Yes. Change the destructor in your base class to be virtual.
virtual ~A(void) {printf("A is deleted\n");}
Call the release member function through a polymorphic pointer in main.
A* b= new B();
b->Release();
btw, your code should be using cout and not printf's.
Getting pedantic, main has never ever returned void so return int.
-Sharad