Inheritance Deletion question

This topic is 4891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
If you make the base object have a virtual destructor then it's perfectly fine. If you don't the results are undefined.

Ah, thank you.

Share on other sites
"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?

Share on other sites
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 on other sites
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 on other sites
According to testing, the virtualized destructor does call both, derived then base.

Share on other sites
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 on other sites
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 on other sites
Either way produces the same result.

Share on other sites
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 hlivingstoneIts 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 on other sites
Quote:
 Original post by snk_kiddestruction (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 on other sites
Quote:
Original post by JohnBolton
Quote:
 Original post by snk_kiddestruction (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]