1, first review the concept of virtual function
Virtual function: Does not mean that a subclass must be implemented, but this function of the subclass can be called by a pointer to the base class.
Pure virtual function: A subclass must be implemented, it defines only a set of behaviors, the class with pure virtual function is an abstract class, can only be used as a base class, cannot be instantiated.
2, why is called "virtual" function?
It is the unpredictability of this function call, which is not deterministic in the compile phase, and the actual address of the function call is not determined until it is executed.
3, why the destructor of a class as a base class must be a "virtual function"?
The destructor of the derived class is called first, and then the destructor of the base class is called. If the destructor is not a virtual function, and the program executes it is also
By destroying the dynamic object of the derived class with a pointer to the base class, only the destructor of the base class is called when the object is destroyed with delete, and the destructor of the derived class is not called
Function. This can result in incomplete destruction of objects and memory leaks.
The understanding of "virtual" in virtual function