15.4.4 virtual destructor
When deleting a pointer to a dynamically allocated object, you need to run the destructor to clear the object before releasing the object's memory. When processing objects in the inheritance level, the pointer type may be different from the dynamic type of the object to be deleted, and the base class type pointer actually pointing to the object of the derived class may be deleted.
If you delete a base class pointer, you need to run the base class destructor and clear the base class members. If the object is actually of a derived type, this behavior is not defined. To run an appropriate destructor, The destructor in the base class must be a virtual one.
If the Destructor is a virtual function, the type of the destructor to run varies depending on the object type.
Like other virtual functions, the virtual functions of the Destructor are inherited. Therefore, if the root class destructor in the hierarchy is a virtual function, the derived class destructor will also be a virtual function, no matter whether the derived class explicitly defines the Destructor or uses the synthetic destructor, the destructor of the derived class are all virtual functions.
Basic destructor is an important exception to the three rules. Rule 3 states that if the class requires destructor, the class almost certainly needs other replication control members. The basic class almost always requires destructor, so as to set the Destructor as a virtual function. If the base class has an empty destructor to set the Destructor as a virtual function, the class also requires a value assignment operator or a copy constructor if the Destructor does not represent it.
Even if the Destructor has no work to do, the root class at the inheritance level should define a virtual destructor.
Constructor and value assignment operator are not virtual functions
In a copy control member, only the Destructor should be defined as a virtual function, and the constructor cannot be defined as a virtual function. The constructor runs before the object is fully constructed. When the constructor runs, the dynamic type of the object is incomplete.
Although operator = can be defined as a virtual function in the base class, this does not affect the value assignment operator used in the derived class. Each class has its own value assignment operator. The value assignment operator in a derived class has a parameter of the same type as the class itself. This type must be different from the type of the value assignment operator of any other class in the inheritance level.
Setting the value assignment operator of a class to a virtual function is very confusing and useless.
15.4.5 virtual functions in constructor and destructor
When constructing a derived class object, first run the base class constructor to initialize the base class of the object. When the base class constructor is executed, the derived class of the object is not initialized. In fact, the object is not a derived class object.
When revoking a derived class object, First undo its derived class part, and then undo its basic class part in reverse order of the construction order.
To adapt to this incompleteness, the compiler treats the object type as changing during construction or destructor. In the base class constructor or destructor, the object of the derived class is treated as the base class type object.
The object type during construction or analysis has an impact on the binding of virtual functions.
If you call a virtual function in a constructor or destructor, the version is defined for the constructor or the Destructor type.
From xufei96's column