We can't inherit constructors, so there is no need for them to be virtual.
A vtable is made for each class having one or more 'virtual functions'. Whenever an object is created of such class, it contains a 'virtual-pointer' which points to the base of corresponding vtable. Whenever there is a virtual function call, the vtable is used to resolve to the function address.
Ctor can not be virtual, because when ctor of a class is executed there is NO vtable in the memory, meaning no virtual pointer defined yet. Hence the ctor should always be non-virtual.
If you think logically about how ctor works and what the meaning/usage of a virtual function is in C++ then you will realize that a virtual ctor would be meaningless in C++. Declaring something virtual in C++ means that it can be overridden by a sub-class of the current class; however the ctor is called when the object is created, at that time you can NOT be creating a sub-class of the class that you must be creating, so there would never be any need to declare a ctor virtual.
@ the answer from the author of C++
No comments:
Post a Comment