C ++ explicitly states that when the derived class object is deleted by a base class pointer and the base class carries a non-virtual destructor, the result is not defined-
In actual execution, the derived component of the object is not destroyed. This will cause a "partial destruction" object. To avoid this problem, it is easy: Give the base class a virtual destructor.
As long as any class has a virtual function, it is almost certain that there should be a virtual destructor.
If the class does not contain virtual functions, it usually indicates that it is not intended to be used as a base class. When the class is not regarded as a base class, the Destructor is
Virtual is often an idea. To implement a virtual function, the object must carry certain information, which is mainly used to determine which virtual function should be called at runtime. This information is usually pointed out by a vptr virtual function table pointer. Vptr points to an array composed of function pointers and becomes a virtual table. Each class with a virtual function has a virtual table. When an object calls a virtual function, the actually called function depends on the vtbl referred to by the vptr of the object. The Compiler looks for an appropriate function pointer in it. Therefore, the size of the object containing the virtual function class will increase the size of a vptr pointer.
Class specialstring: public STD: String {// unknown idea !! STD: string has a non-virtual destructor.
}
If you have no intention of converting a pointer-to-specialstring to pointer-to-string, and then delete the string pointer obtained by the conversion, you are immediately moved to the evil ground of "unclear behavior. The same analysis applies to any class without virtual destructor, including STL containers such as vector, list, set, etc.
Sometimes it is quite convenient to bring a pure virtual destructor to the class. The pure virtual function causes abstract classes. Sometimes you want to have an abstract class
Without the pure virtual function, what should I do? Abstract class is always intended to be used as a base class, And because base class should have a virtual destructor, and the pure virtual function will lead to abstract class, the solution is very simple: declare a pure virtual destructor for the class you want To make abstract.
Class awov {
Public:
Virtual ~ Awov () = 0;
}
Awov ::~ Awov () {}// a part of definition must be provided for this pure virtual destructor
The operating method of the Destructor is that the destructor of the class derived from the deepest layer is called first, and then the destructor of each base class is called. The compiler will create a team in the destructor of the derived class in awov ~ Awov call action, so you must provide a definition for this function. Otherwise, the connector reports an error.
"Give base class a virtual destructor". This rule applies only to polymorphic (with polymorphism) base class. This type of base class
It is designed to "process derived class objects through the base class interface ".
Not all base classes are designed for the purpose of polymorphism. For example, the standard string and STL containers are not designed as base classes, not to mention polymorphism.
Some classes are designed for base class but not for Polymorphism purposes. For example, uncopyable in Clause 6 and input_iterator_tag in the standard library are not designed to "handle derived class objects through the base class interface", so they do not need virtual destructor.