#include <iostream> using namespace std; class Base { private: int* a; public: Base(){ cout<<"base construct new int"<<endl; a = new int(); } //必须申明为虚析构函数,不然子类的析构函数将不会被执行 virtual ~Base(){ cout<<"base destruct del int"<<endl; delete a; } }; class Child : public Base{ private: int* b; public: Child(){ cout<<"Child construct new int"<<endl; b = new int(); } ~Child(){ cout<<"Child destruct del int"<<endl; delete b; } }; int main() { Base* b; b = new Child(); delete b; }
c++虚析构函数
原创
©著作权归作者所有:来自51CTO博客作者fangnux的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
深度解读《深度探索C++对象模型》之C++虚函数实现分析(三)
本系列深入分析编译器对于C++虚函数的底层实现,最后分析C++在多态的情况下的性能是否有受影响,多态究竟有多大的性能损失。
虚函数 虚函数表 虚基类 C++性能分析 C++虚函数实现分析 -
C++ 虚析构函数作用
简单的来说,虚函数的作用是当基类调用派生类的对象时,能够实现多态,即虚函数会优先调用派生类的对应函数。那么虚析构
虚析构函数 #include 虚函数 -
C++的虚函数和虚析构函数
疑问:如何利用一个循环结构,依次处理同一个类族中不同类的对象?如何解决这个
C++ 虚函数 虚析构函数 详解 派生类 -
C++虚基类、虚函数、虚析构函数、纯虚函数
举个例子来说明一下什么是虚基类吧。return 0;}BaseDerived ABaseDerived B从上面
c++ 开发语言 派生类 虚函数 虚函数表