当前位置:网站首页>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 .
边栏推荐
- Rotation, translation and scaling of unity VR objects
- Chapter 11 advanced data management of OpenGL super classic (version 7)
- C # three ways to obtain web page content
- Unity3d position the model, rotate, drag and zoom around the model to obtain the center point of the model
- [note] usage model tree of the unity resource tree structure virtualizingtreeview
- Steamvr causes abnormal scene camera
- Unity3d packaging and publishing APK process
- 力扣977. 有序数组的平方
- 力扣(LeetCode)180. 连续出现的数字(2022.06.29)
- 【VCS+Verdi聯合仿真】~ 以計數器為例
猜你喜欢

Solution to Autowired annotation warning

力扣977. 有序数组的平方

Unity3d realizes Google Digital Earth

Writing unityshader with sublimetext

Pytorch的安装以及入门使用

Malignant bug: 1252 of unit MySQL export

LxC and LXD container summary

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

MinGW-w64下载文件失败the file has been downloaded incorrectly!

Deeply understand the function calling process of C language
随机推荐
Unity automatic pathfinding
Nestjs中控制器和路由的配置使用
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
PWN Introduction (2) stack overflow Foundation
Malignant bug: 1252 of unit MySQL export
【VCS+Verdi聯合仿真】~ 以計數器為例
Redis cluster concept
Win10 vs2015 compiling curaengine
Unity ontriggerenter does not call
How does unity use mapbox to implement real maps in games?
Four methods of unity ugui button binding events
Nestjs配置静态资源,模板引擎以及Post示例
Unity application class and data file path
Chapter 12 pipeline monitoring of OpenGL super classic (version 7)
Golan no tests were run: fmt Printf() &lt; BUG&gt;
中文版PyCharm改为英文版PyCharm
Unity3d position the model, rotate, drag and zoom around the model to obtain the center point of the model
Does the tester need to analyze the cause of the bug?
【VCS+Verdi联合仿真】~ 以计数器为例
C # three ways to obtain web page content