Destructors of Derived classes

M

Mike

I seem to be having a problem with the way destructors are called with
derived classes. Below is a short example of what I'm trying to do.
In the example a is the base class. It has a function 'action' which
destroys the object. As I understand derived classes and destructors,
the destructor of the derived class should be called first, then the
destructor of the base class. Really, for my purposes, order doesn't
matter. In my example here I would only get the output "a:~a". It
never executes the destructor of the derived class.

The code below is just an example to simply show an idea, it hasn't
been compiled.


class a
{
public:
action();
~a();

};

class a_derivative:a
{
public:
~a_derivative();

};


a:~a()
{
printf("a:~a");
}

a:action()
{
delete this;
}


a_derivative:~a_derivative()
{
printf("a_derivative:~a_derivative");
}


main()
{
a_derivative* my_a;

my_a = new a_derivative();

my_a->action();

}
 
A

Alf P. Steinbach

I seem to be having a problem with the way destructors are called with
derived classes. Below is a short example of what I'm trying to do.
In the example a is the base class. It has a function 'action' which
destroys the object. As I understand derived classes and destructors,
the destructor of the derived class should be called first, then the
destructor of the base class. Really, for my purposes, order doesn't
matter. In my example here I would only get the output "a:~a". It
never executes the destructor of the derived class.

The code below is just an example to simply show an idea, it hasn't
been compiled.


class a
{
public:
action();

void action();



virtual ~a();



};

class a_derivative:a
{
public:
~a_derivative();

};


a:~a()
{
printf("a:~a");
}

a:action()
{
delete this;
}


a_derivative:~a_derivative()
{
printf("a_derivative:~a_derivative");
}


main()

int main()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top