[C++] Polymorphism ⑤ (Virtual destructor | Virtual destructor syntax | Virtual destructor meaning | When the parent class pointer points to the child class object, the parent class and the child class use virtual virtual destructor | Code example)

Article directory

  • 1. Virtual destructor
    • 1. The constructor cannot be a virtual function
    • 2. The destructor can be a virtual function
    • 3. Virtual destructor syntax
    • 4. The meaning of virtual destructor
  • 2. Code example – virtual destructor
    • 1. Code example – Failure to use a virtual destructor results in the subclass destructor being unable to be called
    • 2. Code examples – correct examples of using virtual destructors

1. Virtual destructor

1. The constructor cannot be a virtual function

The constructor cannot be defined as a virtual function, cannot be modified with the virtual keyword;

If you want to create an instance object of a subclass, needs to start from the top-level parent class of the subclass, Along the inheritance path, Call the constructors one by one;

Scenario description: Class A is the base class, class B inherits class A, and class C inherits class B;

If you want to create an instance object of class C, you need to start from the top-level parent class A, first call the constructor of A, then call the constructor of B, and finally call the constructor of C;

Refer to [C++] Inheritance ⑧ (class object constructor and destructor calling rules of inheritance + combination mode) blog, constructor/destructor calling strategy, in the case of inheritance + combination, constructor The rules for calling the destructor are as follows:

  • Constructor: parent class -> member -> itself;
    • First, call the parent class constructor;
    • Then, call the member constructor; that is, the constructor of the member variable type;
    • Finally, call your own constructor; self-defined constructor;
  • Destructor: self -> member -> parent class;
    • First, call your own destructor; self-defined destructor;
    • Then, call the member destructor; that is, the destructor of the member variable type;
    • Finally, call the parent class destructor;

2. The destructor can be a virtual function

The destructor can be a virtual function;

The main function of the virtual destructor is: When using the delete operator to release an object, guides the delete budget operator to release dynamic objects;< /font>

Virtual Destructor Virtual Destructor is a special destructor. This function is used to override the destructor of the parent class in the subclass;

Scenario description: Class A is the base class, and class B inherits class A;

Declare a pointer variable of type A and assign it the address of a type B object. When the pointer variable needs to be released, use delete to release the object pointed to by the pointer of type A;

Since the pointer of type A points to an object of type B, the actual type of the object is type B, and B is a subclass of A and has more members than A, so the destructor of type B must be called;

To release the pointer of type A, needs to call the destructor of its subclass B type object, this It is necessary to declare the destructors of type A and type B as virtual destructors;

3. Virtual destructor syntax

The syntax of virtual destructor is Use the virtual keyword in the parent class to declare the destructor;

Virtual destructors must also be used in subclasses;

class Parent
{<!-- -->
public:
// virtual destructor
   virtual ~Base() {<!-- -->}
};

4. The meaning of virtual destructor

Virtual destructor is used in the parent class, In the subclass, must override the parent class’s Virtual destructor, and uses the same function signature;

If the subclass does not provide its own destructor, the compiler will automatically generate a destructor, which will first call the destructor of the parent class and then execute the destructor of the subclass;

The purpose of using virtual destructors is to ensure that resources are released correctly and the destructor is called when releasing subclass objects;< /font>

When using a parent class pointer to point to a subclass object, if you want to release the object pointed to by the pointer through delete,

If it is a normal destructor and no virtual destructor is defined, only the destructor of the parent class will be called, and the destructor of the subclass will not be called;

Virtual destructor can ensure that the destructor of the subclass is called first, and then the destructor of the parent class is called;

This can avoid resource leaks when releasing subclass objects;

It should be noted that only when the destructor of the parent class is a virtual function, the destructor of the subclass must be a virtual function;

If the destructor of the parent class is not a virtual function, The destructor of the subclass can be an ordinary non-virtual function;

2. Code example – virtual destructor

1. Code example – not using a virtual destructor causes the subclass destructor to be unable to be called

In the code below,

  • Declare the subclass pointer to point to the subclass object. When releasing the subclass pointer, first call the subclass destructor and then call the parent class destructor;
  • Declare the parent class pointer to point to the subclass object, and only call the subclass destructor when releasing the parent class pointer;

Code example:

#include "iostream"
using namespace std;

// father
class Parent {<!-- -->
public:
~Parent()
{<!-- -->
cout << "Call the parent class Parent destructor " << endl;
}
};

class Child : public Parent
{<!-- -->
public:
~Child()
{<!-- -->
cout << "Call subclass Child destructor " << endl;
}
};


int main() {<!-- -->

// Declare the subclass pointer pointing to the subclass object
Child* child = new Child();
//When releasing, first call the subclass destructor, then call the parent class destructor
delete child;

//Declare the parent class pointer pointing to the subclass object
Parent* parent = new Child();
//Only call the subclass destructor when releasing
delete parent;
\t
\t
// The console pauses, press any key to continue execution backwards
system("pause");

return 0;
}

Execution results:

Call the subclass Child destructor
Call the parent class Parent destructor
Call the parent class Parent destructor
Please press any key to continue. . . .

2. Code example – correct example of using virtual destructor

In the following code, both the destructors of the parent class and the child class are modified with the virtual keyword;

  • Declare the subclass pointer to point to the subclass object. When releasing the subclass pointer, first call the subclass destructor and then call the parent class destructor;
  • Declare the parent class pointer to point to the child class object. When releasing the parent class pointer, first call the child class destructor and then call the parent class destructor;

Code example:

#include "iostream"
using namespace std;

// father
class Parent {<!-- -->
public:
virtual~Parent()
{<!-- -->
cout << "Call the parent class Parent destructor " << endl;
}
};

class Child : public Parent
{<!-- -->
public:
virtual ~Child()
{<!-- -->
cout << "Call subclass Child destructor " << endl;
}
};


int main() {<!-- -->

// Declare the subclass pointer pointing to the subclass object
Child* child = new Child();
//When releasing, first call the subclass destructor, then call the parent class destructor
delete child;

//Declare the parent class pointer pointing to the subclass object
Parent* parent = new Child();
//Only call the subclass destructor when releasing
delete parent;
\t
\t
// The console pauses, press any key to continue execution backwards
system("pause");

return 0;
}

Execution results:

Call the subclass Child destructor
Call the parent class Parent destructor
Call the subclass Child destructor
Call the parent class Parent destructor
Please press any key to continue. . . .

syntaxbug.com © 2021 All Rights Reserved.