当前位置:网站首页>Virtual and pure virtual destructors and their implementation in c++
Virtual and pure virtual destructors and their implementation in c++
2022-06-25 23:49:00 【smileapples】
Virtual deconstruction and pure virtual deconstruction -----> Solve the problem that the subclass destructor cannot be called
In the use of polymorphism , If there are attributes in the subclass that go to the heap , Then the parent class child needle cannot call the destructor code of the child class when it is released
Solution : Change the destructor in the parent class to virtual or pure virtual destructor
Destructions and pure virtual destructions have something in common :
1、 Can solve the parent class pointer to release the child class object
2、 Need to have specific function implementation
difference :
If it's pure virtual deconstruction , This class belongs to the abstract class , Cannot instantiate object
class Animal{
public:
Animal(){
printf(" Animal class constructor !\n");
}
// Virtual destructions can be used to solve the problem that the parent class pointer releases unclean subclass objects
// virtual ~Animal(){
// cout<<" Animal destructors "<<endl;
// }
// Pure virtual destructors Required declaration , It also needs to be realized
// With pure virtual destructor This class is also an abstract class , Cannot instantiate object
virtual ~Animal() =0;
//
virtual void speak(){
cout<<"hello"<<endl;
}
};
// Implementation of pure virtual destructor , You can release variables opened by the parent class in the heap area
Animal::~Animal(){
cout<<" Animal destructors , Here, release the variables opened by the parent class in the heap !"<<endl;
}
class Dog:public Animal{
public:
string *m_Name;
Dog(string name){
// Subclasses have attributes that open up to the heap
m_Name = new string(name);
cout<<" Subclass "<<*m_Name<<" Constructors !\n";
}
~Dog(){
cout<<" Dog destructor "<<endl;
if (m_Name!=NULL) {
delete m_Name;
m_Name = NULL;
}
}
void speak(){
cout<<" Wang Wang Wang "<<endl;
};
};
void demo(){
// Animal an; // Unable to instantiate the object
Animal *animal = new Dog(" rhubarb ");
// When the parent class pointer is destructed , The destructor of the subclass will not be called , As a result, if the subclass has attributes in the heap , There is a memory leak
animal->speak();
delete animal;
}
int main(int argc, const char * argv[]) {
demo();
return 0;
}
summary
1、 Virtual destructors and pure virtual destructors are used to release subclass objects through parent class pointers
2、 If there is no heap data in the subclass , There is no need for virtual fiction and pure virtual deconstruction
3、 Classes with pure virtual destructors also belong to abstract classes
边栏推荐
- JS中的原型链面试题--Foo和 getName
- excel如何实现中文单词自动翻译成英文?这个公式教你了
- Format the number. If the number is not enough, fill in 0, for example, 1:0001,25:0025
- Blob
- Record the ideas and precautions for QT to output a small number of pictures to mp4
- Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)
- 谷歌浏览器(Chrome)最新v80版本下载
- Graduation trip | recommended 5-day trip to London
- Leetcode-1528- rearrange string - hash table - string
- Summary of common JDBC exceptions and error solutions
猜你喜欢
随机推荐
unsigned与signed之大白话
Classic image segmentation network: UNET supports libtorch deployment reasoning [with code]
18亿像素火星全景超高清NASA放出,非常震撼
第六章 习题(678)【微机原理】【习题】
Graduation trip | recommended 5-day trip to London
中序线索二叉树
树莓派开机发送热点进行远程登录
Kotlin null pointer bug
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
Problems encountered in Doris operation and maintenance
phoenix索引
虚析构和纯虚析构及C ++实现
51 single chip microcomputer, some registers, some knowledge points
产品经理如何把控产品开发的进度
利用swiper实现轮播图
在win10下使用visual studio2015链接mysql数据库
Backup restore of xtrabackup
Can I upload pictures without deploying the server?
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
Analysis on the control condition and mode of go cooperation overtime exit









