Can we have "Virtual Constructors"?
Answers were Sorted based on User's Feedback
Answer / rani
No, we cannot have virtual constructor. Because constructor
is usually use to create instance of a class and we cannot
make it virtual.
| Is This Answer Correct ? | 47 Yes | 2 No |
Answer / vadivel
No, we cannot have virtual constructor because at this time
(during object creation or intialization) virtual table not
yet created.
| Is This Answer Correct ? | 24 Yes | 1 No |
Answer / shashank agrawal
No, This is not possible because virtual is used for avoid
overriding and the constructor is called when the object of
that class has been created so in this case overriding is
not possible then to make the constructor as virtual is
useless.so we dont create it virtal.
| Is This Answer Correct ? | 13 Yes | 0 No |
Answer / bharat
People have already posted the answer in brief, however for
a newbie, this might not be enough... Hence posting this
thread...
If you are referring to constructors as in pure C++, then
there is no such concept as "virtual" constructors. A
constructor is responsible for creation of the particular
object, and hence cannot be "virtual".
It cannot be so, because a virtual function allows the
actual function call to be "deeper" in the hierarchy than
the type through which it is being called. for example, you
can have a base pointer through which a virtual call is made
to a derived object's function code (Derived class is
"deeper" than base class")
Since the constructor is responsible for creation of the
object itself, the function call being "deeper" than the
type being created does not make any sense. Hence a
constructor is always "local", and virtual constructor
concept does not exist.
However, in the domain of design patterns, there is such a
concept as Virtual Constructor or Factory Method. You can
find more information on this concept in "Thinking in C++
Volume 2" (A superb and Free ebook by Bruce Eckel) at the
location
http://www.planetpdf.com/developer/article.asp?ContentID=6634
Hope this is useful...
| Is This Answer Correct ? | 9 Yes | 0 No |
Answer / barun
The pointer to Virtual table which keeps the pointer of
Virtual functions is created within constructor. It means
virtual table is created after constructor is called.So,
there won't be any virtual constructor.
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / vighnesh
Yes there is no virtual constructors.I can think a a reason
may be because a constructor is never inherited so there is
no polymorphism etc involved .
| Is This Answer Correct ? | 10 Yes | 9 No |
Answer / meet
Constructor cannot be virtual. Because C++ is statically typed language. It is meaningless to the C++ compiler to create the object polymorphically. But it is possible to achieve behaviour like virtual constructor. This can be done by using factory design pattern.
Visit this link to know more about virtual constructor:
http://www.geeksforgeeks.org/advanced-c-virtual-constructor/
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / kaydee
Of course there is "Virtual Constructor" in design
pattern's world. This is another name of "Factory Method",
a creational design pattern.
| Is This Answer Correct ? | 3 Yes | 4 No |
Answer / nishikant sahu
of course no such concept lie within C++ ,but we can achieve
this effect. Abstract Factory method show this type of effect.
| Is This Answer Correct ? | 4 Yes | 12 No |
What is setiosflags c++?
What is the last index number in an array of 100 characters a) 100 b) 99 c) 101
What is an ABC: an "Abstract Base Class"?
What is decltype c++?
what is the size of this class class size { public: char data1; double d; int data2; char data3; double data4; short data5; }; please explain the padding for these double variables.
What are the new features that iso/ansi c++ has added to original c++ specifications?
What are c++ storage classes?
What do you know about friend class and friend function?
What is virtual methods?
Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321].
What is the main purpose of c++?
Can you use the function fprintf() to display the output on the screen?