Why constructors cannot be virtual
We already understand how polymorphism works—when a virtual function is called through a pointer or a reference to the base class, that pointer or reference is used to access the v-pointer in the class. The v-pointer is used to identify the true type of the object, that is, the type that the object was created with. It could be the base class itself, or any one of the derived classes. The member function on that object is actually called. So, why can’t the same be done for constructors? Let’s investigate this.
When does an object get its type?
It is pretty easy to understand why the process that we described previously cannot work for creating virtual constructors. First of all, it is evident from the description of the preceding process—as a part of it, we identify the type that the object was created with. That can only happen after the object is constructed—before construction, we don’t have an object...