The sequence of destructor calls for objects is from derived classes to base classes
So what if the base class pointer points to the object of the derived class and performs the destructor correctly?
Let's look at a declaration of a class
Class Subject{public:~subject (); Subject ();}; Class Concretesubject:public Subject{public:concretesubject (); ~concretesubject ();};
Creates a pointer to the base class and deletes the object
Subject * con = new ConcreteSubject ();d elete con;
The result shows that only the destructor of the base class is called ~subject ()
The destructor for the derived class, ~concretesubject (), is not called
If not, the destructor for the base class is set to a virtual function
Then the destructor of the pointed object is not invoked dynamically at execution time
That's the polymorphic nature.
A series of memory problems occur when the object is not properly disposed at run time
Why C + + sets a destructor for a base class as a virtual function