Regarding Virtual Constructor

V

Victor Bazarov

ramesh said:
Why do we have a virtual destructor and why cant we have a virtual
constructor?

Until the object is fully constructed (and its final type known), how
would you decide which constructor function to invoke to construct it?

There is a "virtual constructor" idiom, look it up, but it's not a C++
concept, although it's used in C++.

V
 
S

Sunil Varma

ramesh said:
Why do we have a virtual destructor and why cant we have a virtual
constructor?

An object is constructed on calling the constructor.
Virtual functions are associated to objects.
A constructor is called before the object is constructed.
So you cannot associate a constructor after the object is created.
 
R

Rolf Magnus

Victor said:
Until the object is fully constructed (and its final type known), how
would you decide which constructor function to invoke to construct it?

Well, the final type is always known at compile time when constructing an
object. That's why a virtual constructor makes no sense.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top