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

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!