Jump to content
  • Advertisement
Sign in to follow this  
  • entry
    1
  • comments
    3
  • views
    2878

About this blog

A jourmal on my encounters with programming knols.

Entries in this blog

 

Virtual ~Destructor(){}

I came across the keyword virtual placed next to a destructor in a CPP Ogre code written by my professor. Why and when do we use a virtual destructor ?

We use virtual destructors when somewhere in our code a pointer of a base class points to a derived class. When somewhere later in our code we try to delete this pointer, the destructor that gets called is the base class destructor and not the derived class destructor, which is not what we want since the pointer points to a derived class. This can result in a memory leak. To call the derived class destructor as well, we should make the base class destructor virtual.

grohank

grohank

Sign in to follow this  
  • 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!