About virtual destructors


Virtual destructor is generally produced with polymorphism. The main way of polymorphism is to use the pointer or reference of the base class to point or refer to the derived class to form polymorphism.

But there will be a problem in this way, when we destruct, because it is the pointer of the base class, the constructor of the base class will be called, which will cause the derived memory overflow. To solve this problem, the concept of virtual destructor is introduced. Declare the base class's constructor as virtual, so that it can call the derived class's destructor exactly when the destructor is called.

The following code must use virtual destructor to accurately destruct the derived class and release its occupied memory.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326874685&siteId=291194637