当前位置:网站首页>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
边栏推荐
- 利用swiper实现轮播图
- Qlabel text scrolling horizontally
- 登录拦截器
- Hibernate core api/ configuration file / L1 cache details
- php中使用google protobuf协议环境配置
- The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
- Download the latest V80 version of Google Chrome
- DPVS-FullNAT模式管理篇
- static关键字详解
- c_ uart_ interface_ Example and offboard modes
猜你喜欢
随机推荐
How to generate get/set methods in idea
MySQL InnoDB lock knowledge points
Bi-sql stored procedure (I)
虚析构和纯虚析构及C ++实现
记录一下刷LeetCode瞬间有思路的一道简单题——剑指 Offer 09. 用两个栈实现队列
录屏转gif的好用小工具ScreenToGif,免费又好用!
谈一谈PHP变量或参数的Copy On Write机制
Record the ideas and precautions for QT to output a small number of pictures to mp4
Apache Doris1.0版本集群搭建、负载均衡与参数调优
Hibernate architecture introduction and environment construction (very detailed)
idea Kotlin版本升级
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
WordPress
后序线索二叉树
一文讲透研发效能!您关心的问题都在
c_ uart_ interface_ Example and offboard modes
On the quantity control mechanism of swoole collaboration creation in production environment
史上最简单的录屏转gif小工具LICEcap,要求不高可以试试
Uniapp -- framework arrangement and analysis summary
.user.ini文件导致的php网站安装问题









