博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++中继承 声明基类析构函数为虚函数作用,单继承和多继承关系的内存分布
阅读量:4646 次
发布时间:2019-06-09

本文共 3352 字,大约阅读时间需要 11 分钟。

 

1,基类析构函数不为虚函数

#include "pch.h"#include 
class CBase{public: CBase() { m_one = 0; printf("this is CBase construct\n"); } ~CBase() { printf("this is ~CBase deconstruct\n"); } void setNumOne(int n) { m_one = n; } int getNumOne() { return m_one; }private: int m_one;};class CDrived:public CBase{public: CDrived() { m_two = 0; printf("this is CDrived construct\n"); } ~CDrived() { printf("this is ~CDrived deconstruct\n"); } void setNumTwo(int n) { m_two = n; } int getNumTwo() { return m_two; }private: int m_two;};int main(){ CBase *p = new CDrived; delete p; std::cout << "Hello World!\n"; }

输出:

this is CBase constructthis is CDrived constructthis is ~CBase deconstructHello World!

可以发现继承类析构函数没有调用,若继承类中有一些资源需要释放,则不能释放,故需要将基类析构函数声明为虚函数。

 

#include "pch.h"#include 
class CBase{public: CBase() { m_one = 0; printf("this is CBase construct\n"); } virtual ~CBase() { printf("this is ~CBase deconstruct\n"); } void setNumOne(int n) { m_one = n; } int getNumOne() { return m_one; }private: int m_one;};class CDrived:public CBase{public: CDrived() { m_two = 0; printf("this is CDrived construct\n"); } ~CDrived() { printf("this is ~CDrived deconstruct\n"); } void setNumTwo(int n) { m_two = n; } int getNumTwo() { return m_two; }private: int m_two;};int main(){ CBase *p = new CDrived; delete p; std::cout << "Hello World!\n"; }

输出:

this is CBase constructthis is CDrived constructthis is ~CDrived deconstructthis is ~CBase deconstructHello World!

 

2,

#include "pch.h"#include 
class CBase{public: CBase() { m_one = 0; printf("this is CBase construct\n"); } virtual ~CBase() { printf("this is ~CBase deconstruct\n"); } virtual void setNumOne(int n) { m_one = n; } virtual int getNumOne() { return m_one; }private: int m_one;};class CDrived:public CBase{public: CDrived() { m_two = 0; printf("this is CDrived construct\n"); } ~CDrived() { printf("this is ~CDrived deconstruct\n"); } void setNumTwo(int n) { m_two = n; } int getNumTwo() { return m_two; }private: int m_two;};int main(){ CDrived *p = new CDrived; printf("sizeof(CDrived) = %d\n", sizeof(CDrived)); // 12 delete p; std::cout << "Hello World!\n"; }

 

 

3,多继承

单继承只有一个虚表指针,而多继承往往有多个

#include "pch.h"#include 
class CFather{public: CFather() { } ~CFather() { } virtual void setTall(int tall) { m_tall = tall; }private: int m_tall;};class CMother{public: CMother() { } ~CMother() { } virtual void setWeight(int weight) { m_weight = weight; }private: int m_weight;};class CSon:public CFather,public CMother{public: CSon() { } ~CSon() { } virtual void setAge(int age) // 地址存放在第一个虚表指针后面 { m_age = age; }private: int m_age;};int main(){ CSon cSon; cSon.setAge(8); printf("sizeof(CSon) = %d\n", sizeof(CSon)); // 20 std::cout << "Hello World!\n"; }

 

 

转载于:https://www.cnblogs.com/xiangtingshen/p/11471079.html

你可能感兴趣的文章
JAVA多线程
查看>>
ACE(Adaptive Communication Environment)介绍
查看>>
delphi 更改DBGrid 颜色技巧
查看>>
python编码问题
查看>>
POJ 2031 Building a Space Station
查看>>
面向对象1
查看>>
编程开发之--java多线程学习总结(5)
查看>>
register_globals(全局变量注册开关)
查看>>
as3调用外部swf里的类的方法
查看>>
如何让 zend studio 10 识别 Phalcon语法并且进行语法提示
查看>>
任意阶幻方(魔方矩阵)C语言实现
查看>>
视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
查看>>
第五次作业
查看>>
织梦教程
查看>>
杭电多校 Harvest of Apples 莫队
查看>>
java 第11次作业:你能看懂就说明你理解了——this关键字
查看>>
C/C++心得-结构体
查看>>
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>
数值计算算法-多项式插值算法的实现与分析
查看>>