C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Destructors of Derived classes

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Mike
Guest





PostPosted: Wed Jul 30, 2003 3:44 pm    Post subject: Destructors of Derived classes Reply with 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();
~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





PostPosted: Wed Jul 30, 2003 3:52 pm    Post subject: Re: Destructors of Derived classes Reply with quote



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();



Quote:
~a();

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.