C++实例之虚析构函数
来源:互联网 发布:周杰伦的影响力知乎 编辑:程序博客网 时间:2024/06/15 05:56
(一)父类的析构函数是虚函数时
// Test1.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;class ClxBase{public: ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; }; virtual ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; }; virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };};class ClxDerived : public ClxBase{public: ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; }; ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; }; void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };};/* 情形一// Output from the constructor of class ClxBase!// Do something in class ClxBase!// Output from the destructor of class ClxBase!int _tmain(int argc, _TCHAR* argv[]){ClxBase pTest;pTest.DoSomething();return 0;}*//* 情形二// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxDerived!int _tmain(int argc, _TCHAR* argv[]){ClxBase *pTest = new ClxDerived;pTest->DoSomething();return 0;}*//* 情形三// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxDerived!// Output from the destructor of class ClxDerived!// Output from the destructor of class ClxBase!int _tmain(int argc, _TCHAR* argv[]){ClxBase *pTest = new ClxDerived;pTest->DoSomething();delete pTest;return 0;}*/
/* 情形四 // Output from the constructor of class ClxBase! // Output from the constructor of class ClxDerived! // Do something in class ClxDerived! // Output from the destructor of class ClxDerived! // Output from the destructor of class ClxBase! */ int _tmain(int argc, _TCHAR* argv[]) { ClxDerived pTest; pTest.DoSomething(); return 0; }
(二)父类的析构函数不是虚函数时
// Test2.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;class ClxBase{public: ClxBase() { cout << "Output from the constructor of class ClxBase!" << endl; }; ~ClxBase() { cout << "Output from the destructor of class ClxBase!" << endl; }; virtual void DoSomething() { cout << "Do something in class ClxBase!" << endl; };};class ClxDerived : public ClxBase{public: ClxDerived() { cout << "Output from the constructor of class ClxDerived!" << endl; }; ~ClxDerived() { cout << "Output from the destructor of class ClxDerived!" << endl; }; void DoSomething() { cout << "Do something in class ClxDerived!" << endl; };};/* 情形三// 输出为:// Output from the constructor of class ClxBase!// Output from the constructor of class ClxDerived!// Do something in class ClxDerived!// Output from the destructor of class ClxDerived!*/int _tmain(int argc, _TCHAR* argv[]){ClxBase *pTest = new ClxDerived;pTest->DoSomething();delete pTest;return 0;}
可见如果父类的析构函数没有定义为虚函数的话,当用父类指针指向子类对象的时候,delete父类指针,只会调用父类中的析构函数,不会调用子类中的析构函数。
- c函数之getenv()实例
- c语言实例之函数副本机制
- C++实例之虚析构函数
- C++---------之--------【虚析构函数】
- 实例解析C++/CLI之静态构造函数
- c语言实例之函数求参顺序
- C函数signal实例
- c语言 函数实例
- C实例---函数指针
- c++------------之---【虚析构函数应用】
- linxu c 函数使用实例
- c语言函数实例1
- c实现signal函数实例
- 实例解析C++/CLI之头文件、内联函数与数组
- c没有main函数/java没有main方法--语言运行的实质之实例
- JNI使用实例之C、C++ DLL回调java成员函数
- c语言之cgi实例
- c语言之cgi实例
- 客户端、服务端或功能性的开源框架、开源代码(不断更新中...)
- pdf.js javascript实现pdf阅读器
- ASP.NET IIS7 经典模式和集成模式的区别
- 笔记(referer,获取Ip的地址信息,nginx配置)
- 通过值集名称找到所有用到该值集的并发请求
- C++实例之虚析构函数
- 咋这么烦呢?
- ORACLE日期函数大全
- C基本题_数据类型
- Essential C++学习笔记-------第四章学习笔记
- 常用的值转换
- html
- struts配置文件action中scope引起的问题
- Linux多线程使用实例