 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mike Guest
|
Posted: Wed Jul 30, 2003 3:44 pm Post subject: Destructors of Derived classes |
|
|
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();
}
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Jul 30, 2003 3:52 pm Post subject: Re: Destructors of Derived classes |
|
|
On 30 Jul 2003 08:44:36 -0700, [email]hageland (AT) yahoo (DOT) com[/email] (Mike) wrote:
| Quote: | 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();
| Quote: |
};
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()
| Quote: | {
a_derivative* my_a;
my_a = new a_derivative();
my_a->action();
}
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|