Why a constructor cannot be a virtual function
C++
From the storage space point of view, the virtual function corresponds to avtable
A pointer to a virtual function table, which we all know, but this pointvtable
The pointer is actually stored in the object's memory space. The problem is that if the constructor is virtual, you need to passvtable
To invoke, but the object is not instantiated, that is, the memory space is not yet, how to findvtable
It? So the constructor cannot be a virtual function. In simple terms:the execution of virtual functions depends on the virtual function table. The virtual function table is initialized in the constructor, which is initialized vptr
to point to the correct virtual function table. During the construction of the object, the virtual function table is not initialized and will not be able to proceed.
From the perspective of Use, virtual function is mainly used in the case of incomplete information, can make the overloaded function to get the corresponding call. The constructor itself is to initialize the instance, and it has no practical meaning to use virtual functions. So the constructor is not necessarily a virtual function. The function of a virtual function is to call it through a pointer or reference to the parent class to become the member function that invokes the subclass. The constructor is called automatically when the object is created and cannot be called by a pointer or reference to the parent class, so the constructor cannot be a virtual function. Therefore, it is not necessary to make the constructor a virtual function .
The constructor does not need to be a virtual function, nor is it allowed to be a virtual function, because we always explicitly specify the type of object when creating an object, although we may access it through pointers or references to the base class in the lab. But destruction is not necessarily, we tend to destroy objects through the base class pointers. At this point, if the destructor is not a virtual function, the object type cannot be correctly identified and the destructor cannot be called correctly.
From the implementation, it is vbtl
established after the constructor is called, so the constructor cannot be a virtual function from the actual meaning, the real type of the object cannot be determined when the constructor is called (because the subclass will tune the constructor of the parent class), and the function of the constructor is to provide initialization, The object lifetime is only executed once, not the object's dynamic behavior, nor is it necessary to become a virtual function .
When a constructor is called, one of the first things it does is initialize itVPTR
。 Therefore, it can only know that it is the "current" class, and completely ignores whether there are successors behind the object. When the compiler generates code for this constructor, it generates code for the constructor of the class--is neither a base class nor a derived class for it (because the class does not know who inherits it)。 So it uses theVPTR
It has to be for this class.VTABLE
。 And, as long as it's the last constructor call, in this object's lifetime,VPTR
will remain initialized to point to thisVTABLE
, but if a later derived constructor is called, the constructor sets theVPTR
Point to itVTABLE
, etc. until the end of the constructor function.VPTR
State is determined by the constructor that was last called. (the constructor of the base class is called multiple times, such as itself and its derived classes, and each invocation VPTR
may produce a differentThis is why the constructor call is another reason for the order of the base class to the more derived class. However, when this series of constructor calls is occurring, each constructor is already setVPTR
Point it to its ownVTABLE
。 If a function call uses a virtual mechanism, it will only be generated through its ownVTABLE
The call, not the lastVTABLE
(all constructors are called before the lastVTABLE
)。
inline
,static
,constructor
None of the three functions can bevirtual
Key word.
inline
is the compile-time expansion, must have the entity;
static
Belong toclass
Own, also must have the entity;
virtual
function is based onvtable
(Memory space),constructor
function if it isvirtual
, the call is also subject to thevtable
Looking for, butconstructor
Isvirtual
The case is not found, becauseconstructor
itself does not exist, create aclass
Instances, no instances,class
The members (exceptpublic static/protected static for friend class/functions
, the rest whether or notvirtual
) cannot be accessed.
C++
Those in the function can not be declared as virtual functions
Common can not be declared as virtual functions are: ordinary functions (non-member functions); static member functions; inline member functions; constructors; friend functions.
1. Why c++
does not support normal functions as virtual functions?
Ordinary functions (non-member functions) can only be overload
, cannot be override
, which is declared as a virtual function, has no meaning, so the compiler will statically bind the function at compile time.
-
Why c++
does not support constructors as virtual functions?
The reason is simple, mostly semantically, and therefore not supported. Because constructors are originally created to explicitly initialize object members, however, virtual function
is primarily intended to handle objects correctly without fully understanding the details. In addition, virtual
function is to produce different actions on different types of objects, now the object has not been produced, how to use virtual
function to complete the action you want to complete. (This is not a typical paradox)
Why is an C++
inline member function not supported as a virtual function?
Inline functions are intended to be expanded directly in the code, reducing the cost of function calls, and virtual functions are not unified in order to perform their actions accurately after inheritance. (Again, the inline
function is expanded at compile time, and virtual functions can be dynamically determined at runtime)
Why C++
are static member functions not supported as virtual functions?
Static member functions have only one piece of code for each class, and all of the objects share this code, and he does not need to be dynamically state-determined.
Why is C++
a friend function not supported as a virtual function?
Because the C++
inheritance of friend functions is not supported, there is no virtual function for functions that do not inherit attributes.
Why constructors, inline functions, static functions, and friend functions cannot be virtual functions