C++ 析构函数为虚函数
1、原因:
在实现多态时, 当用基类指针操作派生类, 在析构时候防止只析构基类而不析构派生类。
2、例子:
(1)、
#include<iostream>
using namespace std;
class Base{
public:
Base() {};
~Base() {cout << "Output from the destructor of class Base!" << endl;};
void DoSomething() { cout << "Do something in class Base!" << endl; };
};
class Derived : public Base{
public:
Derived() {};
~Derived() { cout << "Output from the destructor of class Derived!" << endl; };
void DoSomething() { cout << "Do something in class Derived!" << endl; };
};
int main(){
Derived* p = new Derived;
p->DoSomething();
delete p;
return 0;
}
运行结果:
Do something in class Derived!
Output from the destructor of class Derived!
Output from the destructor of class Base!
代码中基类的析构函数不是虚函数,在main函数中用继承类的指针去操作继承类的成员,释放指针P的过程是:先释放继承类的资源,再释放基类资源。
(2)、
#include<iostream>
using namespace std;
class Base{
public:
Base() {};
~Base() {cout << "Output from the destructor of class Base!" << endl;};
void DoSomething() { cout << "Do something in class Base!" << endl; };
};
class Derived : public Base{
public:
Derived() {};
~Derived() { cout << "Output from the destructor of class Derived!" << endl; };
void DoSomething() { cout << "Do something in class Derived!" << endl; };
};
int main(){
Base* p = new Derived;
p->DoSomething();
delete p;
return 0;
}
运行结果:
Do something in class ClxBase!
Output from the destructor of class ClxBase!
代码中基类的析构函数同样不是虚函数,不同的是在main函数中用基类的指针去操作继承类的成员,释放指针P的过程是:只释放基类的资源,而没有调用继承类的析构函数。 调用DoSomething()函数执行的也是基类定义的函数。
一般情况下,这样的删除只能够删除基类对象,而不能删除子类对象,形成了删除一半形象,造成内存泄漏。
在公有继承中,基类对派生类及其对象的操作,只能影响到那些从基类继承下来的成员。如果想要用基类对非继承成员进行操作,则要把基类的这个函数定义为虚函数。 析构函数自然也应该如此:如果它想析构子类中的重新定义或新的成员及对象,当然也应该声明为虚的。
(3)、
#include<iostream>
using namespace std;
class Base{
public:
Base() {};
virtual ~Base() {cout << "Output from the destructor of class Base!" << endl;};
virtual void DoSomething() { cout << "Do something in class Base!" << endl; };
};
class Derived : public Base{
public:
Derived() {};
~Derived() { cout << "Output from the destructor of class Derived!" << endl; };
void DoSomething() { cout << "Do something in class Derived!" << endl; };
};
int main(){
Base* p = new Derived;
p->DoSomething();
delete p;
return 0;
}
运行结果:
Do something in class ClxDerived!
Output from the destructor of class ClxDerived!
Output from the destructor of class ClxBase!
代码中基类的析构函数被定义为虚函数,在main函数中用基类的指针去操作继承类的成员,释放指针P的过程是:释放了继承类的资源,再调用基类的析构函数。调用DoSomething()函数执行的也是继承类定义的函数。
3、总结:
基类指针可以指向派生类的对象(多态性),如果删除该指针delete p;就会调用该指针指向的派生类析构函数,而派生类的析构函数又自动调用基类的析构函数,这样整个派生类的对象完全被释放。如果析构函数不被声明成虚函数,则编译器实施静态绑定,在删除基类指针时,只会调用基类的析构函数而不调用派生类析构函数,这样就会造成派生类对象析构不完全。所以,将析构函数声明为虚函数是十分必要的。
C++ 析构函数为虚函数的更多相关文章
- C++中为什么构造函数不能是虚函数,析构函数是虚函数
一, 什么是虚函数? 简单地说,那些被virtual关键字修饰的成员函数,就是虚函数.虚函数的作用,用专业术语来解释就是实现多态性(Polymorphism),多态性是将接口与实现进行分离:用形象的语 ...
- 转 C++构造函数、析构函数、虚函数之间的关系
C++构造函数.析构函数.虚函数之间的关系 1. 如果我们定义了一个构造函数,编译器就不会再为我们生成默认构造函数了.2. 编译器生成的析构函数是非虚的,除非是一个子类,其父类有个虚析构,此时的函数虚 ...
- C++中继承 声明基类析构函数为虚函数作用,单继承和多继承关系的内存分布
1,基类析构函数不为虚函数 #include "pch.h" #include <iostream> class CBase { public: CBase() { m ...
- C++ 构造函数、析构函数与虚函数的关系
编译环境:windows 10 + VS2105 1.构造函数不能为虚函数 虚函数的作用是为了实现C++多态机制.基类定义虚函数,子类可以重写该虚函数.当子类重写父类虚函数后,父类指针指向子类地址时, ...
- C++析构函数定义为虚函数(转载)
转载:http://blog.csdn.net/alane1986/article/details/6902233 析构函数执行时先调用派生类的析构函数,其次才调用基类的析构函数.如果析构函数不是虚函 ...
- 【转】C++析构函数为什么要为虚函数
注:本文内容来源于zhice163博文,感谢作者的整理. 1.为什么基类的析构函数是虚函数? 在实现多态时,当用基类操作派生类,在析构时防止只析构基类而不析构派生类的状况发生. 下面转自网络:源地址 ...
- 从零开始学C++之虚函数与多态(一):虚函数表指针、虚析构函数、object slicing与虚函数
一.多态 多态性是面向对象程序设计的重要特征之一. 多态性是指发出同样的消息被不同类型的对象接收时有可能导致完全不同的行为. 多态的实现: 函数重载 运算符重载 模板 虚函数 (1).静态绑定与动态绑 ...
- 从零开始学C++之虚函数与多态(二):纯虚函数、抽象类、虚析构函数
一.纯虚函数 虚函数是实现多态性的前提 需要在基类中定义共同的接口 接口要定义为虚函数 如果基类的接口没办法实现怎么办? 如形状类Shape 解决方法 将这些接口定义为纯虚函数 在基类中不能给出有意义 ...
- C++析构函数为什么要为虚函数
注:本文内容来源于zhice163博文,感谢作者的整理. .为什么基类的析构函数是虚函数? 在实现多态时,当用基类操作派生类,在析构时防止只析构基类而不析构派生类的状况发生. 下面转自网络:源地址 h ...
随机推荐
- WCF入门教程系列一
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- java一点内容
1.JAVA有哪些基本数据类型 String是不是基本数据类型 Java语言提供了八种基本类型: 六种数字类型(四个整数型,两个浮点型) 字节型byte 8位 短整型short 16位 整型int 3 ...
- codeforces 340D Bubble Sort Graph(dp,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Bubble Sort Graph Iahub recently has lea ...
- 十进制二进制之间的转化 PHP算法
[ 十进制转二进制 ] function test($var){ $func = function($i){ if($i < 2){ return $i; } $return['int'] = ...
- DLL技术应用04 - 零基础入门学习Delphi47
DLL技术应用04 让编程改变世界 Change the world by program 利用DLL实现窗体重用 利用 Delphi 的 DLL 功能,不但能够实现过程和函数重用,而且还可以实现窗体 ...
- Effective Java实作类别 - 就是爱Java
定义好了Interface之后,现在我们开始来实作类别:Role,建立一个class,implements Role,将3个属性及clone()实作出来,单纯的getter与setter,可以利用ID ...
- 关于WPF中承载 ArcGIS控件。
原文 http://www.cnblogs.com/zoe-j/archive/2011/05/18/2050208.html 之前就做过WPF的应用,之前承载的MapGIS的二次开发控件,今天写一下 ...
- 【剑指offer】面试题25:二叉树中和为某一值的路径
题目: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 思路: dfs一下就可以了.一般dfs肯定递归写比 ...
- 剑指offer-面试题11.数值的整数次方
题目:实现函数double Power(double base,int exponent),求base的 exponent次方.不得使用库函数,同时不需要考虑大数的问题. 这道题看似很简单: 然而需要 ...
- initial pointer [expert c]
initial differece between pointer and array Both arrays and pointers can be initialized with a liter ...