virtual析构函数的作用
C++ Primter中讲“在 C++ 中,基类必须指出希望派生类重写哪些函数,定义为 virtual 的函数是基类期待派生类重新定义的,基类希望派生类继承的函数不能定义为虚函数”。
析构函数是为了在对象不被使用之后释放它的资源,虚函数是为了实现多态。那么把析构函数声明为vitual有什么作用呢?,下面通过例子来说明一下vitual的用途。
using namespace std;
class Base
{
public:
Base() {}; //Base的构造函数
~Base() //Base的析构函数
{
cout << "the destructor of class Base!" << endl;
};
virtual void DoSomething()
{
cout << "Do something in class Base!" << endl;
};
};
class Derived : public Base
{
public:
Derived() {}; //Derived的构造函数
~Derived() //Derived的析构函数
{
cout << "the destructor of class Derived!" << endl;
};
void DoSomething()
{
cout << "Do something in class Derived!" << endl;
};
};
int main()
{
Derived *pTest1 = new Derived(); //Derived类的指针
pTest1->DoSomething();
delete pTest1;
cout << endl;
Base *pTest2 = new Derived(); //Base类的指针
pTest2->DoSomething();
delete pTest2;
return 0;
}
先看程序输出结果:
1 Do something in class Derived!
2 the destructor of class Derived!
3 the destructor of class Base!
4
5 Do something in class Derived!
6 the destructor of class Base!
从运行结果来看,pTest1运行比较正常,也好理解,pTest2运行了子类的Do something和父类的析构函数,并没有运行子类的析构函数,这样会造成内存泄漏。
pTest2运行了子类的Do something量因为父类的Do something加了virtual。
pTest2运行了父类的析构函数是因为父类的析构函数并没有加virtual,所以它并不会运行子类的析构函数。
如果Base的析构函数加上virtual,那么pTest1和pTest2运行过程一样。
总结:如果一个类有可能会被继承,那么它的析构函数最好加上virtual,不然出现内存泄漏问题找都找不到。
virtual析构函数的作用的更多相关文章
- Effective C++ .07 virtual析构函数的提供
主要讲了, 1. virtual析构函数的作用与调用顺序 2. 使用时机,并不是使用了继承就要把基类的析构函数变为虚函数(virtual),只有当用于多态目的时才进行一个virtual析构函数的定义. ...
- C++ 构造函数和析构函数的调用顺序、虚析构函数的作用
构造函数和析构函数的调用顺序 构造函数的调用顺序: 当建立一个对象时,首先调用基类的构造函数,然后调用下一个派生类的构造函数,依次类推,直至到达最底层的目标派生类的构造函数为止. 析构函数的调用书序: ...
- 07——为多态基类声明为virtual析构函数
当基类确定被继承的时候,析构函数声明为virtual是必须的 当返回的派生类的指针或引用的时候,调用析构函数容易发生内存泄漏 当基类作为抽象类使用,声明pure virtual析构函数 析构函数的顺序 ...
- Effective C++ -----条款07:为多态基类声明virtual析构函数
polymorphic(带多态性质的)base classes应该声明一个virtual析构函数.如果class带有任何virtual函数,它就应该拥有一个virtual析构函数. Classes的设 ...
- 条款7:为多态基类声明virtual析构函数
C++明确指出:当派生类对象是由一个基类指针释放的,而基类中的析构函数不是虚函数,那么结果是未定义的.其实我们执行时其结果就是:只调用最上层基类的析构函数,派生类及其中间基类的析构函数得不到调用. # ...
- Effective C++学习笔记 条款07:为多态基类声明virtual析构函数
一.C++明确指出:当derived class对象经由一个base class指针被删除,而该base class带着一个non-virtual析构函数,其结果未定义——实际执行时通常发生的是对象的 ...
- [Effective C++ --007]为多态基类声明virtual析构函数
引言: 我们都知道类的一个很明显的特性是多态,比如我们声明一个水果的基类: class Fruit { public: Fruit() {}; ~Fruit(){}; } 那么我们根据这个Fruit基 ...
- effective c++(07)之为多态基类声明virtual析构函数
class TimeKeeper { public: TimeKeeper() ; ~TimeKepper() ; ... } ; class AtomicClock:public TimeKeepe ...
- [Effective C++系列]-为多态基类声明Virtual析构函数
Declare destructors virtual in polymorphic base classes. [原理] C++指出,当derived class对象经由一个由base clas ...
随机推荐
- ECSHOP模板文件说明
最新ECSHOP 2.7.3完整的一套ECSHOP模板有一下构造 • 35个 .dwt文件(模板框架文件.可调用lbi库文件的主体文件) • 57个 .lbi文件(模板库文件,可通过后台库项目管理直接 ...
- ALV可输入状态下输入金额字段变小数的问题
http://blog.163.com/mxb_sap@yeah/blog/static/10335262520167109022155/ 小数位数两位 当我在给ALV上给该字段输入整数 '12 ...
- cocos2d-x 纹理深入研究
转自:http://blog.csdn.net/qq51931373/article/details/9152227 1.纹理控制. 看此代码: CCSprite *pSprite = CCSprit ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心
B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...
- SQL Sever——无法连接到(local)。“未配置远程连接”和“请求失败或服务未及时响应”
攻克了上篇博客提到的"远程过程调用失败(0x800706be)"的问题. 新的问题接踵而至. . . 一. watermark/2/text/aHR0cDovL2 ...
- [Practical Git] Clean up commits with git rebase
Sometimes its nice to clean up commits before merging them into your main code repo; in this lesson, ...
- Web.xml 中增加log4j
配置文件例如以下.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...
- [Effective C++ --010]令赋值操作符返回一个reference to *this
差不多最经典的就是这个了: x = y = z = ; 解读为: x = (y = ( z = )); 如果没有返回值,上述代码就不能通过编译. 其实看到标题就差不多明白这一条了,但是为什么连续赋值时 ...
- 大型高性能ASP.NET系统架构设计
大型动态应用系统平台主要是针对于大流量.高并发网站建立的底层系统架构.大型网站的运行需要一个可靠.安全.可扩展.易维护的应用系统平台做为支撑,以保证网站应用的平稳运行. 大型动态应用系统又可分为几个子 ...
- QUiLoader 动态加载.ui文件
动态加载UI文件是指,用 Qt Designer 通过拖拽的方式生产.ui 文件.不用 uic工具把.ui 文件变成等价的 c++代码,而是在程序运行过程中需要用到UI文件时,用 QUiLoader ...