Series: Subject: Topic:
Question: 240 of 339

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

Asked by: virsa | Member Since Oct-2006 | Asked on: Oct 15th, 2006

View all questions by virsa   View all answers by virsa

Showing Answers 1 - 10 of 10 Answers
Renjith Mamman

Answered On : Oct 17th, 2006

Constructor is local to the class and we can't override the class in derived classpls see the link

Login to rate this answer.

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

Login to rate this answer.
vijay saxena

Answered On : 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.method:class xyz{public: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.

Login to rate this answer.

Answered On : Nov 6th, 2006

Thanks a lot.

Login to rate this answer.

I think base class constructor is available in derived class.when we create derived class object,base class constructor gets called first.after that derived class constructor gets called.pls correct me if i am wrong.

Login to rate this answer.

Answered On : Mar 29th, 2007

View all answers by dasam

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.

Login to rate this answer.

Answered On : May 21st, 2007

Why don't we have virtual constructors? C++ GlossaryA 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

Login to rate this answer.

Answered On : Nov 16th, 2008

View all answers by mucdull

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.

Login to rate this answer.

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.

Login to rate this answer.
Jos Collin

Answered On : 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.

Login to rate this answer.

Give your answer:

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

Related Open Questions



twitter fb Linkedin GPlus RSS


Interview Question

 Ask Interview Question?


Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.