1.why constructor can't be virtual?2.what's the difference between destuctor and virtual destructor?

Questions by virsa   answers by virsa

Showing Answers 1 - 10 of 10 Answers

Renjith Mamman

  • Oct 17th, 2006

constructor is local to the class and we can't override the class in derived classpls see the linkhttp://blogs.msdn.com/oldnewthing/archive/2004/05/07/127826.aspx

  Was this answer useful?  Yes

Constructor is called at the time of object creation, & there, u always know "what is the class type?"... Constructors are local to class. In case of destructor, we need to call destructor on the basis of type of object, hence it should be virtual. you need a virtual destructor every time u use a base class pointer to access derived class object (dynamically), & then use it to call derived class destructor. consider following example:
class base
base(){cout << "nthis is base construtor";
virtual ~base() {cout << "nthis is base distructor";
class derived
derived(){cout << "n this is derived constructor";
~derived(){cout << "n this is derived destructor";
int main()
base *p = new derived;
delete p;

When we run this program, it would print:
this is base constructor.
this is derived constructor.
this is derived destructor.
this is base destructor.

Look at the order carefully, base class constructor is called first & its destructor is called at last... Now consider if compiler allows us to make constructor virtual, then in that case when we call "base *p = new derived"; No constructor of base class would be called; which is an error in itself...
However in case of destructors (as they are called in reverse order of creation), if we don't have declared it as virtual, only base class destructor would have been called... which we don't want here....
Moreover, the constructors & destructors can't be "pure virtual", as it would make the derived class as abstract base classes (as they can't overwrite constructor/destructor of base class)....
I hope I am clear

  Was this answer useful?  Yes

vijay saxena

  • Oct 24th, 2006

constructor and destructor are local member function of class , constructor invokes automatically whenever its instance is created and destructor destroys their initialised data, space, array etc.


class xyz{


xyz(){cout<<"this is constructor"<<endl;}

~xyz(){cout<<"this is destructor"<<endl;}


void main(){xyz t();//constructor calling


note: no need to call destructor function.

since constructor is local to the class and it can't be accessible by derived class so constructor cannot be virtual.

  Was this answer useful?  Yes


  • Nov 6th, 2006

Thanks a lot.

  Was this answer useful?  Yes


  • Mar 29th, 2007

A constructor cannot be virtual because at the time when the constructor is invoked the virtual table (vtable) would not be available in the memory. Hence we cannot have a virtual constructor.

A virtual destructor is one that is declared as virtual in the base class and is used to ensure that destructors are called in the proper order.

Virtual methods should be used judiciously as they are slow due to the overhead involved in searching the virtual table. They also increase the size of an object of a class by the size of a pointer.

  Was this answer useful?  Yes


  • May 21st, 2007

Why don't we have virtual constructors?
C++ Glossary
A virtual call is a mechanism to get work done given partial information. In particular, "virtual" allows us to call a function knowing only an interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the exact type of what you want to create. Consequently, a "call to a constructor" cannot be virtual

  Was this answer useful?  Yes


  • Nov 16th, 2008

Constructors are called at object creation time and complete information about the object is known so the compiler knows exactly which constructors to call. Also in a derived object constructors in the base classes are called before the constructor in the most derived object. As such, constructor cannot be virtual because having a virtual constructor would mean the order of construction can be altered which will break object initialization.

A virtual destructor is a destructor that can be overriden by a derived class. This is necessary because destruction of an object goes in the reverse order of construction - destructors in most the derived class is called first. If a destructor is not virtual, static binding will cause a destructor in some base class of an object to be called first, leading to a situation where some destructors in the more derived classes of an object to not be called, which can result in improper destruction of an object.

  Was this answer useful?  Yes

There is nothing like Virtual Constructor. The Constructor can’t be virtual as the constructor is a code which is responsible for creating an instance of a class and it can’t be delegated to any other object by virtual keyword means.

  Was this answer useful?  Yes

Jos Collin

  • Oct 4th, 2011

That's not correct. The VTable will be created completely during compile time itself. The reason why constructor can't be virtual is that the virtual mechanism will work only on completely constructed objects. At the time when the constructor get called, the object is not completely created. So there cannot be virtual constructors.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions