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

What happens to an instance of derived class if I delete the ptr to the base class?

This topic is 5154 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

Guest Anonymous Poster
If you use new to allocate memory for a derived class and then assign the derived object to a base class pointer and then delete it what will happen depends if the base class has a virtual destructor or not. If it does, then every derived class from the base class will have a destructor and when any pointer such a class object is delete''d all memory allocated should be returned, but if the base class did not defined a virtual destructor, then only the base classes destructor will get called, none of the derived classes, and memory will be leaked.

Share this post

Link to post
Share on other sites