当前位置:网站首页>虚析构和纯虚析构及C ++实现
虚析构和纯虚析构及C ++实现
2022-06-25 22:08:00 【smileapples】
虚析构和纯虚析构----->解决子类析构函数调不到的问题
在多态的使用时,如果子类中有属性开辟到堆区,那么父类子针在释放时无法调用到子类的析构代码
解决方式:将父类中的析构函数改为虚析构或者纯虚析构
析构和纯虚析构共性:
1、 可以解决父类指针释放子类对象
2、 都需要有具体的函数实现
区别:
如果是纯虚析构,该类属于抽象类,无法实例化对象
class Animal{
public:
Animal(){
printf("动物类构造函数!\n");
}
// 利用虚析构可以解决父类指针释放子类对象不干净的问题
// virtual ~Animal(){
// cout<<"动物类析构函数"<<endl;
// }
// 纯虚析构函数 既需要声明,也需要实现
// 有了纯虚析构 该类也是抽象类了,无法实例化对象
virtual ~Animal() =0;
//
virtual void speak(){
cout<<"hello"<<endl;
}
};
// 纯虚析构的实现,可以释放父类在堆区开辟的变量
Animal::~Animal(){
cout<<"动物类析构函数,在此释放父类在堆区开辟的变量!"<<endl;
}
class Dog:public Animal{
public:
string *m_Name;
Dog(string name){
// 子类有属性开辟到堆区
m_Name = new string(name);
cout<<"子类"<<*m_Name<<"构造函数!\n";
}
~Dog(){
cout<<"狗类析构函数"<<endl;
if (m_Name!=NULL) {
delete m_Name;
m_Name = NULL;
}
}
void speak(){
cout<<"汪汪汪"<<endl;
};
};
void demo(){
// Animal an; // 无法实例化对象了哦
Animal *animal = new Dog("大黄");
// 父类指针在析构的时候,不会调用子类的析构函数,导致如果子类在堆区有属性,出现内存泄漏
animal->speak();
delete animal;
}
int main(int argc, const char * argv[]) {
demo();
return 0;
}
总结
1、虚析构和纯虚析构就是用来解决通过父类指针释放子类对象
2、如果子类中没有堆区数据,可以不用虚虚构和纯虚析构
3、拥有纯虚析构函数的类也属于抽象类
边栏推荐
- Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
- C# IO Stream 流(一)基础概念_基本定义
- util. Collection and encapsulation of JS tool functions
- C# IO Stream 流(二)扩展类_封装器
- Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)
- Bi-sql stored procedure (I)
- Record the ideas and precautions for QT to output a small number of pictures to mp4
- excel如何实现中文单词自动翻译成英文?这个公式教你了
- A. Balance the Bits--Codeforces Round #712 (Div. 1)
- Leetcode (605) -- flower planting
猜你喜欢

The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high

C# IO Stream 流(一)基础概念_基本定义

unsigned与signed之大白话

Uniapp -- the use of document collation and push of unipush

UE4 learning record 2 adding skeleton, skin and motion animation to characters

Visual studio code create minimal web API (asp.net core)

mysql版本升级+数据迁移

Reprint: detailed explanation of qtablewidget (style, right-click menu, header collapse, multiple selection, etc.)

第六章 习题(678)【微机原理】【习题】

Style setting when there is a separator in the qcombobox drop-down menu
随机推荐
Kotlin null pointer bug
Understanding of pseudo classes
二进制、16进制、大端小端
WordPress
(serial port Lora module) centrida rf-al42uh private protocol test at instruction test communication process
解析产品开发失败的5个根本原因
Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
谷歌浏览器(Chrome)最新v80版本下载
idea Kotlin版本升级
Ad20 learning notes II
Solve 'tuple' object has no attribute 'lower‘
C# IO Stream 流(二)扩展类_封装器
UE4 learning record 2 adding skeleton, skin and motion animation to characters
CXF
DPVS-FullNAT模式部署篇
The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high
Bi-sql stored procedure (I)
Analyse des cinq causes profondes de l'échec du développement de produits
CSDN force value
Today's 61 Fu