By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,009 Members | 1,643 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,009 IT Pros & Developers. It's quick & easy.

Why no virtual constructor in C++ ?

P: 2
Hi,
I want to know about virtual constructor in C++.
I know there is no such feature in C++. But why????
What are the reasons of that????

Thanks & Regards
Krishna
Apr 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: 1
A constructor can not be virtual because at the time when constructor is invoked the vittual table would not be available in the memory .hence we can not have virtual constructor .
Aug 26 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
Also 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.
Aug 26 '06 #3

P: 1
Hi,
I want to know about virtual constructor in C++.
I know there is no such feature in C++. But why????
What are the reasons of that????

Thanks & Regards
Krishna
When ever we are creating an object for a class using new operator, then at that point of time we should know what type of object we are creating, the new operator will invoke the constructor so if we don't know the type of the constructor then it is not possible to create that so because of this resone virtual constructor in not possible.
Apr 4 '07 #4

P: 1
Actually the virtual method table exists for each class not or the instances of the Class. Many languages do Have virtual Constructors. Delphi does for Example (I think C# does also). It allows you to call the proper constructor on a class reference.

if it existed in C++ it would be kind of like this

class Dog
{
virtual Dog();
....
}
class Poodle: public Dog
{
virtual Poodle();
...
}

class Rotweiler: public Dog
{
virtual Rotweiler();
...
}
class UglyPoodle:public Poodle
{
virtual UglyPoodle();
//...
}

typdef DogClass = class of Dog; //thats Delphi'ish notation

Dog* DogFactory(DogClass DogKind)
{
return new DogKind;
}

int main()
{
Dog* D = DogFactory(Poodle);
}

//this of course can be done without class references but in some cases this is much easier and more elegant. consider a complicated class hierarchy of Controls, WindowedControls, Buttons etc. you can easily construct the right kind of Object without complicated switch statements or anything
}
May 24 '07 #5

P: 7
i just wanted to know "What is virtual table?"
Dec 18 '07 #6

Meetee
Expert Mod 100+
P: 931
i just wanted to know "What is virtual table?"
Whenever a program has a virtual function declared, a v-table is constructed for the class. The v-table consists of addresses to the virtual functions for classes and pointers to the functions from each of the objects of the derived class. Whenever there is a function call made to the c++ virtual function, the v-table is used to resolve to the function address.

For more search on google.
Dec 18 '07 #7

P: 7
can you tell me the creation and executing procedure for functions other than virtual functions?
Dec 19 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.