Search code examples
c++oopconstructorpolymorphismvirtual

Virtual constructors


I was wondering what is the meaning of a virtual constructor and how would it be used.

In addition I know that C++ does not allow for a virtual constructor, and I was wondering why.


Solution

  • C++ does not allow virtual constructors because you need an object to invoke a virtual method in the first place!

    The term virtual constructor is used for for idiom and a well-known design pattern. This idiom/pattern involves the definition of factory: an intermediate object with a virtual method who's role is to create the object in question. Because the method is virtual and it's purpose is to create an object, it is nicknamed a "virtual constructor."