#Previous question #Index of C++ Questions #Next question

Virtual destructor in base class ensures that the destructor of derived class will be called when using base pointer to the object.

Suppose we have a base class and a derived class

class base 
{
  public: base()
  {
    cout << "base construction";
  }
  ~base()
  {
    cout << "base destruction";
  }
};
class derived : public base
{
  char *name;
  public : derived ()
  {
    cout << "derived construction";
	name = new char[10];
	cout << "mem allocated for name";
	
  }
  ~ derived ()
  {
    cout << "derived destruction";
	delete [] name;
	cout << "mem de-allocated for name";
  }
};
int main (int argc, char *argv[])
{
  base *b = new derived();
  delete b;
}

output:

base construction
derived construction
mem allocated for name
base destruction

Here derived destruction is missing. Thus derived destructor is not getting called by compiler as it is early/compile time bind. As the type of the object is base, only base destructor is getting called. This also leads to memory leak as string member name is not getting de-allocated.

To ensure proper destructor sequence call we must make the base destructor virtual. This ensures dynamic/runtime binding of the destructor through vtable mechanism After modifying the prototype ~base() to virtual ~base() the sequence will be as expected as below.

class base 
{
  public: base()
  {
    cout << "base construction";
  }
  virtual ~base() 
  {
    cout << "base destruction";
  }
};
Output after this change:
base construction
derived construction
mem allocated for name
derived destruction
mem de-allocated for name
base destruction

Similarly in future if we need to inherit child_derived from derived, we must make the destructor i.e. ~derived() as virtual. Thus it is a good practice to make destructor always virtual. This ensures proper calling sequence of destructors as it gets inherited down in the child classes.

A good C++ editor always takes care of this. VC++ editor always makes destructor as virtual when it is created using wizard or template.

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14