虚析构函数
来源:互联网 发布:电视定时开关机软件 编辑:程序博客网 时间:2024/05/17 01:44
#include <iostream> using namespace std;class A{public: A() { cout << "构造A!" << endl; }; virtual ~A() { cout << "析构A!" << endl; }; virtual void DoSomething() { cout << "A的DoSomething!" << endl; };};class B : public A{public: B() { cout << "构造B!" << endl; }; ~B() { cout << "析构B!" << endl; }; void DoSomething() { cout << "B的DoSomething!" << endl; };};int main(){ A *Test1 = new A; cout << "**************" << endl; Test1->DoSomething(); cout << "**************" << endl; delete Test1; cout << "**************" << endl; B *Test2 = new B; cout << "**************" << endl; Test2->DoSomething(); cout << "**************" << endl; delete Test2; cout << "**************" << endl; A *Test3 = new B; cout << "**************" << endl; Test3->DoSomething(); cout << "**************" << endl; delete Test3; return 0;}
运行结果:
修改代码,删除virtual
1 #include <iostream> 2 3 4 using namespace std; 5 6 class A 7 { 8 public: 9 A() { cout << "构造A!" << endl; };10 ~A() { cout << "析构A!" << endl; };11 12 virtual void DoSomething() { cout << "A的DoSomething!" << endl; };13 };14 15 class B : public A16 {17 public:18 B() { cout << "构造B!" << endl; };19 ~B() { cout << "析构B!" << endl; };20 21 void DoSomething() { cout << "B的DoSomething!" << endl; };22 };23 24 int main()25 {26 A *Test1 = new A;27 cout << "**************" << endl;28 Test1->DoSomething();29 cout << "**************" << endl;30 delete Test1;31 cout << "**************" << endl;32 B *Test2 = new B;33 cout << "**************" << endl;34 Test2->DoSomething();35 cout << "**************" << endl;36 delete Test2;37 cout << "**************" << endl;38 A *Test3 = new B;39 cout << "**************" << endl;40 Test3->DoSomething();41 cout << "**************" << endl;42 delete Test3;43 return 0;44 }
运行结果:
如果是: B b;
运行结果:
0 0
- 虚析构函数、纯虚析构函数
- 虚析构函数,纯虚析构函数~~~
- 构造函数 虚函数 虚析构函数
- 虚析构函数、纯虚析构函数、虚构造函数
- 纯虚函数、虚函数、虚析构函数
- 虚析构函数、纯虚析构函数、虚构造函数
- 虚函数,虚析构函数,虚函数表
- 虚析构函数、纯虚析构函数、虚构造函数
- 虚析构函数
- 虚析构函数
- 虚析构函数
- 虚析构函数
- 虚析构函数
- 虚析构函数
- 虚析构函数
- 虚析构函数
- 虚析构函数
- 虚析构函数
- 错误error LNK2019: 无法解析的外部符号 _auxDIBImageLoadW@4
- Redis 起步
- [迅为开发板资料分享]分享一个迅为4412开发板OTG烧录批处理文件
- c++解决 {ctor}构造函数不能有返回类型或者无故缺少“;”(在“using”的前面)的问题
- HeapAlloc与malloc的区别
- 虚析构函数
- HttpClient用POST上传文件
- linux 安装配置java环境
- Annotation自定义注解
- 文字匹配解析例子(读取Json 格式配置文件)
- TCP Incast学习之中遇到的问题
- 遗传算法解决TSP问题
- 聊天登陆之注册界面
- isodata算法确定k均值聚类的k值