Jump to content
  • Advertisement

Archived

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

owl

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

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

Advertisement
If it''s the base class has a virtual destructor everything is fine. If it doesn''t, bad things could happen.

Share this post


Link to post
Share on other sites
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
Yes, it seems pretty obvious now I think about it. For some reason I was unable to figure that out by myself. Thanks again.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!