Sign in to follow this  
Telastyn

Inheritance Deletion question

Recommended Posts

Telastyn    3777
I'm reading through the inheritance/abstraction tutorials on cplusplus.com and something is not terribly clear. Since base pointers can be assigned to derived objects, what happens when delete is used with the base pointer. For example:
base     *bptr;
derived  *dptr;

dptr=new derived;
bptr=dptr;
delete bptr;
Does that actually delete the entire derived object? Does it toss an error? Does it half-delete the object, causing issues? Is that a generally undefined behavior?

Share this post


Link to post
Share on other sites
Telastyn    3777
Well, I suppose that creates another question... [which I can experiment to find the answer myself if it's not answered by the time I can...]

Once you virtualize the destructor, does the base destructor still get called when the derived object is destroyed, or does the redefinition replace it?

Share this post


Link to post
Share on other sites
hlivingstone    163
Its been a while since ive used inheritance like this (b/c its relitivly slow) but yeah the destructors should be called, trickeling down the virtual pointer list... starting from the base object and going down through each derivied object in order, up to the last objects destructor. or maybe its the other way around, sorry i kinda forget.

can someone else refresh me? thx

Share this post


Link to post
Share on other sites
hlivingstone    163
ah okay,

how did you trigger it, did you call:
delete bptr;

or

delete dptr;

off hand it seems to me that if you call delete with the base, it will trickel down, and if you call delete with the derived pointer the destructor calls will propogate up.

aggg i forget

Share this post


Link to post
Share on other sites
JohnBolton    1372
Destuctors in C++ are not virtual by default. You must make a destructor virtual if you want the destructors in derived classes to be called when a base class is destroyed.

It is generally a good idea to always make a destructor virtual to avoid problems in the future. The exception would be the case where an additional 4 bytes added to the size of the class is a problem.

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by hlivingstone
"If you make the base object have a virtual destructor then it's perfectly fine. If you don't the results are undefined."

Arnt all destructors virtual by default?


Nope.

Quote:
Original post by hlivingstone
Its been a while since ive used inheritance like this (b/c its relitivly slow)


I'm not sure what that is suppose to mean, if you don't make the base destructor virtual and you have a pointer of the base type pointing to an instance of a derived type allocated on the heap then try to delete it only the base's destructor will be invoked.

Quote:
Original post by hlivingstone
but yeah the destructors should be called, trickeling down the virtual pointer list... starting from the base object and going down through each derivied object in order, up to the last objects destructor. or maybe its the other way around, sorry i kinda forget.

can someone else refresh me? thx


Actually the order is the other way round,

construction starts from base to derived and

destruction (when the base's destructor is virtual) is from derived to base.

Share this post


Link to post
Share on other sites
JohnBolton    1372
Quote:
Original post by snk_kid
destruction (when the base's destructor is virtual) is from derived to base.


Destruction is always derived then base. virtual/non-virtual determines the starting point.

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by JohnBolton
Quote:
Original post by snk_kid
destruction (when the base's destructor is virtual) is from derived to base.


Destruction is always derived then base. virtual/non-virtual determines the starting point.


That is the case if you just have an instance of a derived type, but if you read carefully what the question asked by hlivingstone it was in reference to calling delete on a base pointer pointing to an instance of a derived type allocated on the heap.

[Edited by - snk_kid on September 30, 2004 5:07:59 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this