Archived

This topic is now archived and is closed to further replies.

Polymorphism - Delete Object

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have done something that looks like the following: class Mine{ Mine(); ~Mine(); int IDNumber } class MineTwo:public Mine{ MineTwo(); ~MineTwo(); int SecondIDnumber; } int main(void){ MineTwo *a = new MineTwo; Mine *b = (Mine*)a; delete a; } Ok so in that, I create a Class, Convert the pointer to another type, then run the delete function on that pointer... Does this mean that the "~MineTwo()" function is not called? (my comp seems to not call it) I seem to be having trouble with this. Any other ways to work around this problem? Assume that i do not know what type of class it was to begin with as I am trying to write a dynamic Library... Thanks --Josh

Share this post


Link to post
Share on other sites
It means that you have undefined behaviour. An attempt to delete a derived class via a pointer to base results in undefined behaviour if the destructor is non-virtual.

You might also like to note that your upcast is redundant. You don''t need to say:

Mine *b = (Mine*)a;


Since MineTwo *IS A* Mine you can implicitly cast a to b.

--
The placement of a donkey''s eyes in its head enables it to see all four feet at all times.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
It means that you have undefined behaviour. An attempt to delete a derived class via a pointer to base results in undefined behaviour if the destructor is non-virtual.

You might also like to note that your upcast is redundant. You don''t need to say:

Mine *b = (Mine*)a;


Since MineTwo *IS A* Mine you can implicitly cast a to b.

--
The placement of a donkey''s eyes in its head enables it to see all four feet at all times.


Results are defined : destructor of the static type will be used. virtual simply says to use the dynamic type.

I am just being picky

Share this post


Link to post
Share on other sites
quote:
Original post by Gorg
Results are defined : destructor of the static type will be used. virtual simply says to use the dynamic type.

I am just being picky


You are just being wrong. The C++ Standard states it is undefined behaviour.

Share this post


Link to post
Share on other sites
first off, if anyone bothered to read the code. you would realize that you are deleting a which is MineTwo anyway. i think you meant to delete b which is of the base class mine.

anyway, the compiler will (assuming those functions are NOT virtual) call the destructor of what the current varible is. ie, you cast to a mine, it calls ~mine(). this can be seen through a simple compile, and is NOT the desired result. unfortunatly the compiler CANNOT be magically and know that you want to call ~minetwo from a mine casted variable. this is why when using polymorphism you use virtual functions.

ANY class that is a base class MUST have a virtual destructor or things wont work as intended. with virtual a deconstructor, ~minetwo() and ~mine() will be called, as they should.

though this was all (well maybe not as elaborated) by steg in the first response.

Share this post


Link to post
Share on other sites