Can we have "Virtual Constructors"?

Answers were Sorted based on User's Feedback



Can we have "Virtual Constructors"?..

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

Can we have "Virtual Constructors"?..

Answer / shivangi

there is nothing like "virtual constructor"

Is This Answer Correct ?    48 Yes 4 No

Can we have "Virtual Constructors"?..

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

Can we have "Virtual Constructors"?..

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

Can we have "Virtual Constructors"?..

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

Can we have "Virtual Constructors"?..

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

Can we have "Virtual Constructors"?..

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

Can we have "Virtual Constructors"?..

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

Can we have "Virtual Constructors"?..

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

Can we have "Virtual Constructors"?..

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

Post New Answer

More C++ General Interview Questions

Is c++ the hardest language?

0 Answers  


What are the basics of local (auto) objects?

0 Answers  


What is a friend function in c++?

0 Answers  


How do you establish a has-a relationship?

0 Answers  


What is the latest version on c++?

0 Answers  






What is pair in c++?

0 Answers  


Are there interfaces in c++?

0 Answers  


Describe functional overloading?

6 Answers   HP,


What is this weird colon-member (" : ") syntax in the constructor?

0 Answers  


an operation between an integer and real always yeilds a) integer result b) real result c) float result

0 Answers  


Define Virtual function in C++.

0 Answers   iNautix,


What is basic if statement syntax?

0 Answers  


Categories