[C ++ in-depth analysis] 41. Constructor, can the destructor be a virtual function?

1 The constructor cannot be a virtual function, the destructor can be a virtual function

Constructor cannot be a virtual function

  • After the execution of the constructor, the virtual function table pointer can be properly initialized

Destructors can become virtual functions

  • It is recommended to declare the destructor as a virtual function when designing the class

Programming experiment: construction, destructuring, virtual function

// 41-1.cpp
#include<iostream>
using namespace std;
class Base
{
public:
    Base()
    {
        cout << "Base()" << endl;
    }
    virtual ~Base()
    {
        cout << "~Base()" << endl;
    }
};
class Derived : public Base
{
public:
    Derived()
    {
        cout << "Derived()" << endl;
    }
    virtual ~Derived()
    {
        cout << "~Derived()" << endl;
    }
};
int main()
{
    Base* p = new Derived();
    delete p;
    return 0;
}

The pointer of the parent class points to the object of the subclass, new Derived (); Create an object of the subclass will call the constructor of the parent class and the constructor of the subclass, p is the pointer of the parent class, if the destructor is not a virtual function, Will call the destructor of the parent class, will not call the constructor of the subclass.
The destructor is defined as a virtual function. According to the polymorphic attributes, the object pointed to is destructed. When the subclass Derived is destructed, the subclass destructor will be called first and then the parent class destructor.

Compile and run:

$ g++ 41-1.cpp -o 41-1
$ ./41-1
Base()
Derived()
~Derived()
~Base()

2 Constructing a destructor to call a virtual function does not occur polymorphism

No polymorphic behavior in the constructor

  • When the constructor is executed, the virtual function table pointer is not initialized correctly

No polymorphic behavior in the destructor

  • When the destructor is executed, the virtual function table pointer has been destroyed

Therefore, no polymorphic behavior occurs in the constructor and destructor, only the function version defined in the current class is called ! ! !

will

// 41-2.cpp
#include<iostream>
using namespace std;
class Base
{
public:
    Base()
    {
        cout << "Base()" << endl;
        func();								// 调用虚函数
    }
    virtual void func()						// 增加虚函数
    {
        cout << "Base::func()" << endl;
    }
    virtual ~Base()
    {
        func();								// 调用虚函数
        cout << "~Base()" << endl;
    }
};
class Derived : public Base
{
public:
    Derived()
    {
        cout << "Derived()" << endl;
        func();								// 调用虚函数
    }
    virtual void func()						// 增加虚函数
    {
        cout << "Derived::func()" << endl;
    }
    virtual ~Derived()
    {
        func();								// 调用虚函数
        cout << "~Derived()" << endl;
    }
};
int main()
{
    Base* p = new Derived();
    delete p;
    return 0;
}

Compile and run, no polymorphic behavior occurs in the constructor and destructor, only call the function version defined in the current class

$ g++ 41-2.cpp -o 41-2
$ ./41-2
Base()
Base::func()
Derived()
Derived::func()
Derived::func()
~Derived()
Base::func()
~Base()

3 Summary

1. The constructor cannot become a virtual function, and the destructor can become a virtual function.
2. Both the constructor and the destructor cannot generate polymorphism.

Published 298 original articles · praised 181 · 100,000+ views

Guess you like

Origin blog.csdn.net/happyjacob/article/details/104485667