The reason the constructor cannot be declared as a virtual function is:
Explanation 1: the so-called virtual function is to execute only one program under polymorphism. In terms of the concept of inheritance, it is always necessary to construct a parent class object before being a subclass object, if the constructor is set to a virtual function, You have to display the call constructor when constructing the parent class. Another reason is to prevent errors, imagine if you accidentally rewrite a function in the subclass that is the same as the parent class constructor, then your parent class constructor will be overwritten, that is, the parent class cannot be constructed. an error occurs.
Explanation 2: The main significance of a virtual function is that it is inherited by a derived class to generate polymorphism. in the constructor of a derived class, the compiler adds the code for constructing the base class. If the base class constructor uses parameters, the derived class must provide parameters for the base class in the initialization list of its constructor, this is the reason.
Someone recently asked whether the constructor can be a virtual function:
Of course not.
Explanations:
1. From the perspective of storage space
Virtual functions correspond to a vtable. We all know that this vtable is actually stored in the object's memory space. The problem arises. If the constructor is virtual, it needs to be called through vtable, but the object has not been instantiated, that is, the memory space is not yet available. How can we find vtable? Therefore, the constructor cannot be a virtual function.
2. From the usage perspective
A virtual function is used to call a function that is overloaded with incomplete information. The constructor itself is to initialize the instance, so it does not actually make sense to use the virtual function. Therefore, the constructor does not need to be a virtual function.
=:
The role of a virtual function is to call the member function of the subclass when calling it through the pointer or reference of the parent class. The constructor is automatically called when an object is created. It cannot be called through the pointer or reference of the parent class. Therefore, the constructor cannot be a virtual function.
III.
The constructor does not need to be a virtual function or a virtual function, because we always need to specify the object type when creating an object, although we may access it through the pointer or reference of the base class in the lab
However, the analysis structure is not necessarily. We often destroy objects through the pointer of the base class. At this time, if the Destructor is not a virtual function, it cannot correctly identify the object type and thus cannot call the Destructor correctly.
IV:
From the implementation point of view, vbtl is established after the constructor is called, so the constructor cannot become a virtual function.
In terms of actual meaning, the real type of the object cannot be determined when calling the constructor (because the child class calls the constructor of the parent class); and the role of the constructor is to provide initialization, an object is executed only once in its life cycle. It is not a dynamic behavior of the object, nor is it necessary to become a virtual function.
V:
When a constructor is called, one of its first tasks is to initialize its v p t r. Therefore, it can only know that it is a "current" class, and ignore whether there is a successor behind this object. When the compiler generates code for this constructor, it generates code for the constructor of this class-neither the base class nor, it is not a derived class for it (because the class does not know who inherits it ).
Therefore, the V p t r used must be for the V ta B L E of this class. Furthermore, as long as it is the final constructor call, during the lifetime of this object, V p t r will be initialized to point to this V ta B L E. However, if another later-derived constructor is called, this constructor will set V p t r to point to its V ta B L E. until the final constructor ends. The status of V p t r is determined by the final called constructor. This is another reason why constructor calls are derived from the base class to the more class order.
However, when this series of constructor calls occur, each constructor has set V p t r to point to its own V ta B L E. If a function call uses a virtual mechanism, it will only generate a call through its own V ta B L E, instead of the final v ta B l e (only after all constructors are called will the final v ta B L E ).
The function of setting the Destructor as a virtual function:
Explanation: In class inheritance, if a base class Pointer Points to a derived class, when the base class pointer is deleted, if it is not necessarily a virtual function, the derived part of the derived class cannot be analyzed.
# Include "stdafx. H"
# Include "stdio. H"
Class
{
Public:
A ();
Virtual ~ A ();
};
A: ()
{
}
A ::~ A ()
{
Printf ("delete class AP/N ");
}
Class B: public
{
Public:
B ();
~ B ();
};
B: B ()
{
}
B ::~ B ()
{
Printf ("delete class BP/N ");
}
Int main (INT argc, char * argv [])
{
A * B = new B;
Delete B;
Return 0;
}
Output result: delete Class B
Delete Class
If you remove a's virtual:
Then it becomes Delete Class.