About virtual destruction
来源:互联网 发布:淘宝店铺手机版模板 编辑:程序博客网 时间:2024/05/19 01:58
构造函数的调用顺序是:先基类后派生类
析构函数的调用顺序是:先派生类后基类
一个类的对象析构时,只能调用到其基类的析构函数,无法调用到其派生类的析构函数。当遇到基类对象析构需要调用到其派生类的析构函数时,将析构函数声明为virtual函数就可以解决这个问题。
测试代码:
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class Base
{
public:
Base()
{
}
virtual ~Base()
{
Print();
}
virtual void Print()
{
cout<< "Base : release";
}
};
class T1:public Base
{
public:
T1()
{
}
virtual ~T1()
{
Print();
}
virtual void Print()
{
cout << "T1 : release";
}
};
class T2:public T1
{
public:
T2()
{
}
virtual ~T2()
{
Print();
}
virtual void Print()
{
cout << "T2 : release ";
}
};
int _tmain(int argc, _TCHAR* argv[])
{
{
Base T1;
T1.Print();
}
Base* p = new T2;
delete p;
{
T1 p;
p.Print();
}
system("pause");
return 0;
}
- About virtual destruction
- Item9: Never call virtual functions during construction or destruction
- 【条款9】Never call virtual functions during construction or destruction
- About Virtual Machine
- About C++ "Virtual"
- Something about virtual function and pure virtual function
- ios About the Virtual Memory System
- Item 9: 绝不要在 construction(构造)或 destruction(析构)期间调用 virtual functions(虚拟函数)
- 条款09:绝不在构造和析构过程中调用virtual函数(Never call virtual functions during construction or destruction.)
- About install linux ON virtual PC by use of VMware
- The error “Pure Virtual Function Called” about C++
- Some tips about Abstract base classes and pure virtual functions
- Detail about How VPTR and Virtual table works
- Detail about How VPTR and Virtual table works
- [翻译] Effective C++, 3rd Edition, Item 9: 绝不要在 construction(构造)或 destruction(析构)期间调用 virtual functions(虚拟函数)
- Destruction (Episode 121)
- Android MediaExtractor Construction & Destruction
- King of Destruction HDU
- 胡言乱语
- Visual Studio2005 常用快捷键
- 一不小心,进入了晚婚的行列!!
- 北极光合伙人邓锋:要别人跳火坑,你自己先跳
- 如何利用java得到当前的时间和前一天的时间
- About virtual destruction
- 在struts框架下实现文件的上传
- Gtk+ 基础
- 在struts1.1框架下,利用smartupload实现文件的上传(可以是多个文件)
- [数据库]MYSQL数据库常用命令集合
- moon.png
- 简单工厂模式
- Google 浏览器的简单使用
- uClinux 在EasyARM2200开发板上的运行