Why constructor can not be virtual (rpm)

Source: http://blog.sina.com.cn/s/blog_620882f401016ri2.html

 

1, from the perspective of the storage space

    Virtual function corresponds to a vtable, which we all know, but this vtable pointer is actually stored in the memory space objects. Problems arose, if the constructor is virtual, it is necessary to call by vtable, but the object has not been instantiated, that memory is never, can not find the vtable , so the constructor can not be virtual.

2, the angle from the use

       Virtual function is mainly used in the case of incomplete information, the override function can obtain the corresponding call. The constructor to initialize itself instance, the use of virtual functions that have no practical significance ah. Therefore, a constructor is a virtual function is not necessary.

Virtual functions that effect through a pointer or reference to the parent class when it is possible to call the member function calls into subclasses. The constructor is called automatically when you create an object, it is impossible to call the parent class by a pointer or reference to , and therefore will not be predetermined virtual constructor function.

4,, vbtl built from the point of view achieved only after the constructor call, so the constructor can not be a virtual function  

 From a practical point of view meaning, the true type to call the constructor was also unable to determine the object (because the constructor subclass will bring up the parent class); and the role of the constructor is to provide initialization is performed only once in the lifetime of the object, not the object dynamic behavior, there is not much need to be a virtual function

5, when a constructor is called, one of the first things it does is to initialize its vptr . Therefore, it can only know that it is the "current" category , while completely ignoring the object later if there successor. When the compiler generates code for the constructor, which is to generate code for the constructor of the class - - neither for the base class, nor is it derived class (class because it does not know who inherited).

    It VPTR must be used for the class V TA BLE. And, as long as it is the last constructor call, then the lifetime of the object, VPTR will remain initialized to point to the V TA BLE, but then if there is a later derived constructor is called, the constructor set point VPTR its turn V TA BLE, etc. until the last constructor end. VPTR state is determined by the last constructor is invoked (Annotation: This should be and "polymorphic in Subclasses override the parent class, then the function corresponding to the virtual function table of the elements will change") . This is why the constructor calls another reason to be more classes derived from the base class to order .

       However, when this series of constructor calls taking place, each constructor have been set VPTR points to its own V TA BLE. If you use a virtual function call mechanism, it generates through its own V TA BLE calls only, not the last V TA BLE (all constructors will be called after the last V TA BLE) (comment: Reserved doubt, etc. may later look will understand).

Guess you like

Origin www.cnblogs.com/Stephen-Qin/p/11512835.html