为什么C++基类析构函数写成虚函数
2015-07-25 15:12
399 查看
下面的代码举例:
// virtual.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string.h>
#define MAXLEN 128
class CEmployee{
public:
int m_ID;
char m_Name[MAXLEN];
char m_Depart[MAXLEN];
CEmployee(){
printf("CEmployee 类构造函数被调用了/n");
}
~CEmployee(){
printf("CEmployee 类析构函数被调用了/n");
getchar();
}
protected:
private:
};
class COperator:public CEmployee{
public:
char m_Password[MAXLEN];
COperator(){
strcpy(m_Name,"MR");
printf("COperator 子类的构造函数被调用了/n");
getchar();
}
~COperator(){
printf("COperator 子类析构函数被调用/n");
getchar();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CEmployee *oper = new COperator() ;
delete oper;
return 0;
}
函数的返回结果是:
可以看到值调用了父类的析构函数,而子类的析构函数没有被调用,那么可想而知,如果在子类的构造函数中对某个成员函数在堆空间中分配了空间,而之类没有被调用,是不是会造成内存泄漏呢?答案是肯定的,那有什么办法可以解决这种情况下出现的内存泄漏呢?那就是把父类的析构函数写为虚函数,看下面的代码:
// virtual.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string.h>
#define MAXLEN 128
class CEmployee{
public:
int m_ID;
char m_Name[MAXLEN];
char m_Depart[MAXLEN];
CEmployee(){
printf("CEmployee 类构造函数被调用了/n");
}
virtual ~CEmployee(){
printf("CEmployee 类析构函数被调用了/n");
getchar();
}
protected:
private:
};
class COperator:public CEmployee{
public:
char m_Password[MAXLEN];
COperator(){
strcpy(m_Name,"MR");
printf("COperator 子类的构造函数被调用了/n");
getchar();
}
~COperator(){
printf("COperator 子类析构函数被调用/n");
getchar();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CEmployee *oper = new COperator() ;
delete oper;
return 0;
}
运行结果:
因此,在写父类的时候,最好将其析构函数写为虚函数。这样可以防止比较瘾避的内存泄漏。
// virtual.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string.h>
#define MAXLEN 128
class CEmployee{
public:
int m_ID;
char m_Name[MAXLEN];
char m_Depart[MAXLEN];
CEmployee(){
printf("CEmployee 类构造函数被调用了/n");
}
~CEmployee(){
printf("CEmployee 类析构函数被调用了/n");
getchar();
}
protected:
private:
};
class COperator:public CEmployee{
public:
char m_Password[MAXLEN];
COperator(){
strcpy(m_Name,"MR");
printf("COperator 子类的构造函数被调用了/n");
getchar();
}
~COperator(){
printf("COperator 子类析构函数被调用/n");
getchar();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CEmployee *oper = new COperator() ;
delete oper;
return 0;
}
函数的返回结果是:
可以看到值调用了父类的析构函数,而子类的析构函数没有被调用,那么可想而知,如果在子类的构造函数中对某个成员函数在堆空间中分配了空间,而之类没有被调用,是不是会造成内存泄漏呢?答案是肯定的,那有什么办法可以解决这种情况下出现的内存泄漏呢?那就是把父类的析构函数写为虚函数,看下面的代码:
// virtual.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string.h>
#define MAXLEN 128
class CEmployee{
public:
int m_ID;
char m_Name[MAXLEN];
char m_Depart[MAXLEN];
CEmployee(){
printf("CEmployee 类构造函数被调用了/n");
}
virtual ~CEmployee(){
printf("CEmployee 类析构函数被调用了/n");
getchar();
}
protected:
private:
};
class COperator:public CEmployee{
public:
char m_Password[MAXLEN];
COperator(){
strcpy(m_Name,"MR");
printf("COperator 子类的构造函数被调用了/n");
getchar();
}
~COperator(){
printf("COperator 子类析构函数被调用/n");
getchar();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CEmployee *oper = new COperator() ;
delete oper;
return 0;
}
运行结果:
因此,在写父类的时候,最好将其析构函数写为虚函数。这样可以防止比较瘾避的内存泄漏。
相关文章推荐
- C++那些细节--static_cast,dynamic_cast,const_casst,reinterpret_cast
- C++ 的那些坑 (Day 0)
- Online Judge System For SzNOI 题库 语法百题 c++ d040
- Online Judge System For SzNOI 题库 语法百题 c++ d039
- Online Judge system For SzNOI 题库 语法百题 c++ d038
- Online Judge System For SzNOI 题库 语法百题 c++ d037
- Online Judge System For SzNOI 题库 语法百题 c++ d035
- Online Judge System For SzNOI 题库 语法百题 c++ d036
- Online Judge System For SzNOI 题库 语法百题 c++ d034
- Online Judge System For SzNOI 题库 语法百题 c++ d033
- Online Judge System For SzNOI 题库 语法百题 c++ d032
- C++线程池
- Online Judge System For SzNOI 题库 语法百题 c++ d031
- C++虚函数表分析
- PAT (Basic Level) Practise (中文)1039. 到底买不买(20) C语言
- 计算导论与C语言基础(专项课程之一)
- C++回忆录:(一)new和malloc的区别
- C++常见面试题
- MFC、VC、C++、QT、QTcreater和QTSDK、OSG、OpenGL和OSGEARTH 作者zhangke95
- extern “C”