Why C++ does not have Virtual Constructors?

Questions by someshwar_goli   answers by someshwar_goli

Editorial / Best Answer

chethanhb  

  • Member Since Feb-2010 | Feb 25th, 2010


I am one of the person who has pricked my head in understanding the above question. Let me explain in my way...

If you have virtual function or (let me take just "virtual" keyword as of now), it means that this member function can be redefined by derived class. When we have a virtual function in a class, it will have vtable (virtual table). This vtable will have the address of the virtual function which is defined in derived.
Now the point is, how you invoke the virtual function which is defined in derived class with the help of object of the class?
Once you create an object of class, vptr (virtual pointer) will be created. Which inturn will be pointing to the base address of the vtable. So now it is clear that you can invoke the virtual funtion of derived class only with an object that has created.
Now coming to virtual constructor,
If we make constructor as virtual in base, it means that it could be redefined in derived. Keep in mind that constructor is invoked during object creation (object is not created yet. still it is in the status "creating". Object will create only after executing constructor part code). Assume you are trying to create object of the class which has virtual constructor. During this process constructor of the class will be invoked. It looks for virtual keyword. Now it tries to look for virtual constructor in derived. But not possible bcz there is no vptr and no vtable avaibale at this point of time. So, when object is not created, then there is no vptr. If no vptr for this object, then how the consturtor of derived is invoked?. No address of this construtor will available in vtable.
Hence there is no point in having virtual constructor.

I believe that i have cleared your doubt.

Showing Answers 1 - 25 of 25 Answers

Shrutimitra

  • Oct 30th, 2006
 

if you think logically about how constructors work and what the meaning/usage of a virtual function is in C++ then you will realise that a virtual constructor would be meaningless in C++.Declaring something virtual in C++ means that it can be overridden by a sub-class of the current class, however the constructor is called when the objected is created, at that time you can not be creating a sub-class of the class you must be creating the class so there would never be any need to declare a constructor virtual.

Srutiranjan Rath

  • Nov 14th, 2006
 

I want to know about this query

  Was this answer useful?  Yes

sravan

  • Nov 18th, 2006
 

constructers r mainly used for initializing var,memory allocation.  virtual means in future it is possible to re-define, for that purpose we can put VIRTUAL before functions.But there is no use of redefining the constructers.So we can't use virtual before constructers.

kalayama

  • Dec 5th, 2006
 

I agree with ShruthiMitra.It doesn't make sense to have virtual constructor and constructed never needs to overridden.

  Was this answer useful?  Yes

Amar

  • Dec 12th, 2006
 

In C++, Virtual is Used foe polymorphism, which is calling a Derived class function using the Base class pointer. Since all the functions in the Public, protected are inherited we may have virtual functions. BUT BUT Constructor of a class is never and ever extended by a derived, hence we will never heed to have a Virtual constructor.....

That is U will never need to call a derived class constructor using the Base class Pointer...

muralikannan

  • Feb 13th, 2007
 

Greeting !!As our folks told, we don't really required virtual at declaration stage of construction, but might required at creational stage to avoid duplicate.That's also so called Virtual Construction - Ref: Herbit Sch book (C++ Complete Ref thrid rd part).

  Was this answer useful?  Yes

Fast Eddie

  • Apr 2nd, 2007
 

All construction is inline, hence no virtual construction.

  Was this answer useful?  Yes

Chiranjeevi

  • Jun 8th, 2007
 

From mypoint of view:

A fully constructed object is required inorder to invoke virtual member function of class.
As we know constructor gets called during object getting constructed,
If constructor is marked as virtual mean that it also required fully constructed object to invoke but object is still under construction.

Salim

  • Aug 23rd, 2007
 

A virtual-table(vtable) is made for each Class having one or more 'virtual-functions'. Whenever an Object is created of such class, it contains a 'virtual-pointer' which points to the base of corresponding vtable. Whenever there is a virtual function call, the vtable is used to resolve to the function address.
 
    Constructor can not be virtual, because when constructor of a class is executed there is no vtable in the memory, means no virtual pointer defined yet. Hence the constructor should always be non-virtual.

paul.anuj

  • Aug 5th, 2008
 

i think u all guys are assuming wrong.

           constructors have the same name as its class name. and if we declare constructor as virtual, then it shoud be redefined in its derived class with the same name, but you can not have the same name of two classes. so it is not possible to have virtual constructor.

  Was this answer useful?  Yes

chethanhb

  • Feb 25th, 2010
 

I am one of the person who has pricked my head in understanding the above question. Let me explain in my way...

If you have virtual function or (let me take just "virtual" keyword as of now), it means that this member function can be redefined by derived class. When we have a virtual function in a class, it will have vtable (virtual table). This vtable will have the address of the virtual function which is defined in derived.
Now the point is, how you invoke the virtual function which is defined in derived class with the help of object of the class?
Once you create an object of class, vptr (virtual pointer) will be created. Which inturn will be pointing to the base address of the vtable. So now it is clear that you can invoke the virtual funtion of derived class only with an object that has created.
Now coming to virtual constructor,
If we make constructor as virtual in base, it means that it could be redefined in derived. Keep in mind that constructor is invoked during object creation (object is not created yet. still it is in the status "creating". Object will create only after executing constructor part code). Assume you are trying to create object of the class which has virtual constructor. During this process constructor of the class will be invoked. It looks for virtual keyword. Now it tries to look for virtual constructor in derived. But not possible bcz there is no vptr and no vtable avaibale at this point of time. So, when object is not created, then there is no vptr. If no vptr for this object, then how the consturtor of derived is invoked?. No address of this construtor will available in vtable.
Hence there is no point in having virtual constructor.

I believe that i have cleared your doubt.

masterofcse

  • Mar 12th, 2010
 

For argument sake, Virtual functions are same for all objects in a class. Why can't those functions be maintained as static in nature.


Please don't be confuse with static here. My idea is to keep virtual table is in common location and access since this is same across all objects.

Why do we need to create a VTable for each object. One VTable is sufficient for a class.

We can have VPTR which is static and be constructed by compiler.

  Was this answer useful?  Yes

surendrap

  • Nov 21st, 2011
 

since we know that name of constructor and name of class is always same . and in c++ the name of class always unique, so the concept of overriding can not be apply on constructor.

  Was this answer useful?  Yes

manoj kumar

  • Sep 26th, 2012
 

when we write a function virtual, a virtual table is created and to point to that virtual table we need virtual pointers so as we have not created the pointers for the constructor we cant make the constructors virtually.
so the virtual destructors is possible to make but not the virtual constructors.

  Was this answer useful?  Yes

Vikram

  • May 14th, 2015
 

What if I want my constructor to have different behavior then its default?

  Was this answer useful?  Yes

Jaya

  • Jul 17th, 2015
 

What about parameterized constructor... It also contain the same name.

  Was this answer useful?  Yes

JOYJIT DEB

  • Jul 19th, 2015
 

The constructor initializes the object and thereby the vtable.
When constructor of a class is invoked, the vtable is not yet created, thereby negating the point of having a virtual constructor.

  Was this answer useful?  Yes

Pankaj Bhardwaj

  • Aug 28th, 2015
 

Virtual table is created at compile time. so when constructor is executed virtual table will be there, but I guess vptr is not initialised.

  Was this answer useful?  Yes

nag

  • Dec 21st, 2015
 

Virtual pointer is starting 2 bytes of the class objects when virtual functions are there in class. So virtual pointer is created when constructor is called. During initialization process only virtual pointer is initialized to V-Table. So virtual concept will not applied on virtual constructor.

  Was this answer useful?  Yes

Subhashish

  • Jan 18th, 2016
 

Constructor Can not be virtual. Its simple... When We make a function as virtual and when we instantiate that class (creating an object), its VTABLE will be created which stores the address of virtual function, now who creates this VTABLE, constructor only, so When we make constructor itself as virtual, so there is no entity left who can create VTABLE for this constructor. Hence We cannot make constructor as virtual.

  Was this answer useful?  Yes

Tsvi

  • Jan 21st, 2016
 

Virtual method call needs an object with assigned vtable, and when a constructor is called there is not yet an object.

  Was this answer useful?  Yes

EigenCritic

  • Apr 6th, 2016
 

I will try to explain why there is no good reason we should have virtual constructors
Virtual functions are used for calling the derived method using a base pointer (or from inside the base class).
A virtual constructor would actually create a Derived object when calling the Base() constructor.

Code
  1. base->virtual_method();  // the method from Derived class is used if implemented

  2. // Considering how C++ constructors works

  3. Base* base = new Derived(); // you get a Derived object stored in a Base pointer

  4. // Assuming virtual constructors would exist and Base would have a virtual constructor, then

  5. Base* base = new Base(); //  you get a Derived object stored in a Base pointer; but now its confusing

  Was this answer useful?  Yes

vikash tiwary

  • May 11th, 2017
 

Virtual functions are used in order to invoke functions based on the type of object pointed by the pointer. But is not "invoked", it is called only once when it is declared. so, a constructor cannot be virtual.

  Was this answer useful?  Yes

Venkataramakrishna

  • Mar 22nd, 2018
 

Whenever a class object is created this pointer is created just before initializing class constructor member values. If the constructor is virtual, you cannot create this pointer.

  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