当前位置:网站首页>Virtual and pure virtual destructions
Virtual and pure virtual destructions
2022-06-30 05:09:00 【Just call me Xiao Qin】
introduction
This article will explain why Polymorphic property base class Of Destructors are declared as virtual functions , And what is Pure virtual destructors .

virtual Destructor
Resource leakage occurs
Sometimes we want to release subclass objects with the help of a parent class pointer ::
class Person
{
public:
Person();
~Person();
...
};
class Student : public Person
{
... };
// We use the base class pointer to point to the derived class object and release
Person* ptr = new Student;
...
delete ptr;
C++ Made it clear , When a derived class object is deleted via a base class pointer , The destructor of the base class is not a virtual function , The result is undefined .
- What usually happens during actual execution is that the derived components of the object are not destroyed
- That is to say, it is declared on
StudentThe member variables in the class are not destroyed - However, its base class components will be destroyed , This has led to a strange resource leak
solve
The solution to this problem is very simple , Just declare the destructor of the base class as a virtual function . such delete Base class pointer , Will destroy the entire object , Including all derived components .
Virtual functions : Namely be
virtualModified class member functions are called virtual functions .
class Person
{
public:
Person();
virtual ~Person();
...
};
class Student : public Person
{
... };
Person* ptr = new Student;
...
delete ptr;
here , When the destructor is called , First, we will find the virtual table pointer in the object , Find the appropriate function pointer in the virtual table pointed to by the virtual table pointer and call .
summary :
- A base class with polymorphism should declare a
virtualDestructor . If the base class has anyvirtualfunction , It should have onevirtualDestructor - Class is designed for use other than as a base class , Or not for polymorphism , Should not be declared as
virtualDestructor
pure virtual Destructor
Write... After the virtual function =0 , Then this function is a pure virtual function . Classes that contain pure virtual functions are called abstract classes , Abstract classes cannot instantiate objects . A derived class cannot instantiate an object after inheritance , Only rewrite pure virtual functions , A derived class can instantiate an object .
Conventional pure virtual functions
Because classes containing pure virtual functions cannot instantiate objects , So pure virtual functions generally have no function body .
- This pure virtual function cannot be called without an object , So realization has no value , Usually not implemented
You might think of calling with a pointer to a class type , Let's take a look at this method :
class Animal
{
public:
virtual void Run() = 0;
void Fly();
};
void Animal::Run() {
cout << "Run()" << endl; }
void Animal::Fly() {
cout << "Fly()" << endl; }
Animal* ptr = nullptr;
ptr->Fly(); // Program normal screen printing Fly()
ptr->Run(); // The program crash screen does not print
Why does this happen ? Is there any difference between ordinary function and pure virtual function ?
This is because the virtual function needs to be called through the virtual table pointer , No object, no virtual table pointer , So the above code will crash .
Pure virtual deconstruction
Sometimes we need an abstract class , But there is no pure virtual function , What to do ? Because abstract classes are always used as base classes , And the base class should have a virtual Destructor , And because pure virtual functions will produce abstract classes , So we can : Declare a class that you want to be abstract pure virtual Destructor .
class Animal
{
public:
virtual ~Animal() = 0;
};
// It has to be for this pure virtual Function provides a definition , Otherwise, the connector will report an error
Animal::~Animal() {
}
The operation logic of the destructor is , The destructor of the deepest derived class is called first , Then the destructor of each base class is called . The compiler will be there Animal Create a pair in the destructor of a derived class of ~Animal Call action of , So you have to provide a definition for this function . otherwise , The connector will report an error .
边栏推荐
- 0 foundation starts self-study unit notes control direction becomes larger
- Win10 vs2015 compiling curaengine
- GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;
- Very nervous. What should I do on the first day of software testing?
- Special folders in unity3d and their meanings
- 东塔攻防世界—xss绕过安全狗
- Chinese pycharm changed to English pycharm
- Pytorch的安装以及入门使用
- Oculus quest2 development: (I) basic environment construction and guide package
- Operation of JSON file
猜你喜欢

PWN入门(2)栈溢出基础

Unity scroll view element drag and drop to automatically adsorb centering and card effect

PWN Introduction (2) stack overflow Foundation

Some books you should not miss when you are new to the workplace
![[notes] unity Scrollview button page turning](/img/c7/47c4056871d0212ac61524539f0d0e.jpg)
[notes] unity Scrollview button page turning

Procedural animation -- inverse kinematics of tentacles

Parkour demo

redis集群概念

【VCS+Verdi联合仿真】~ 以计数器为例

pycharm 数据库工具
随机推荐
东塔攻防世界—xss绕过安全狗
Ugui uses its own function to realize reverse mask
Pycharm database tool
Nestjs入门和环境搭建
Chapter 10 of OpenGL super classic (7th Edition) calculation shader
力扣59. 螺旋矩阵 II
z-index属性在什么情况下会失效?
Unreal 4 unavigationsystemv1 compilation error
LXC 和 LXD 容器总结
Tcp/ip protocol details Volume I (Reading Guide)
Nestjs introduction and environment construction
PWN入门(2)栈溢出基础
The difference between SVG and canvas
【VCS+Verdi聯合仿真】~ 以計數器為例
Untiy3d controls scene screenshots through external JSON files
Unity scroll view element drag and drop to automatically adsorb centering and card effect
C # equipment synthesis
力扣589:N 叉树的前序遍历
Unity automatic pathfinding
0 basic unity course. Bricklaying