栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Unix/C/C++进阶--通过基类释放子对象

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unix/C/C++进阶--通过基类释放子对象

Unix/C/C++进阶--通过基类释放子对象
  • 1 概述
  • 2 情况分类
    • 2.1 父类指针,父类对象
    • 2.2 子类指针,子类对象 【父类析构函数为虚函数】
    • 2.3 父类指针,子类对象
      • 父类析构函数为虚函数,删除父指针
      • 父类析构函数为虚函数,父指针做子类型转换后再删除
      • 父类析构函数不实现,删除父指针
      • 父类析构函数不实现,父指针做子类型转换后再删除
  • 3 结构体
  • 4 详解
  • 参考

1 概述

如下图,子类继承父类,delete就是依据指针指向的内存地址以及虚表指针。

2 情况分类

具体删除内存多少与指针类型和虚表指针相关。

2.1 父类指针,父类对象

正常释放自身。

#include 
#include 
using namespace std;

class Father
{
public:
    Father() { cout << "father" << endl; }
    // virtual ~Father() {}
    virtual ~Father() { cout << "~father" << endl; }
};


class Son: public Father
{
public:
    Son()  { cout << "son" << endl; }
    ~Son() {  cout << "~son" << endl; }
};

int main()
{
    Father father;
}
  • 结果
father
~father
2.2 子类指针,子类对象 【父类析构函数为虚函数】

父类析构函数为虚函数,释放子类自身以及父类。

#include 
#include 
using namespace std;

class Father
{
public:
    Father() { cout << "father" << endl; }
    // virtual ~Father() {}
    virtual ~Father() { cout << "~father" << endl; }
};


class Son: public Father
{
public:
    Son()  { cout << "son" << endl; }
    ~Son() {  cout << "~son" << endl; }
};

int main()
{
    Son son;
}
  • 结果
father
son
~son
~father
2.3 父类指针,子类对象 父类析构函数为虚函数,删除父指针
#include 
#include 
using namespace std;

class Father
{
public:
    Father() { cout << "father" << endl; }
    // virtual ~Father() {}
    virtual ~Father() { cout << "~father" << endl; }
};


class Son: public Father
{
public:
    Son()  { cout << "son" << endl; }
    ~Son() {  cout << "~son" << endl; }
};

int main()
{
    Father *father = new Son();
    delete father;
}
  • 结果
father
son
~son
~father
父类析构函数为虚函数,父指针做子类型转换后再删除
#include 
#include 
using namespace std;

class Father
{
public:
    Father() { cout << "father" << endl; }
    // virtual ~Father() {}
    virtual ~Father() { cout << "~father" << endl; }
};


class Son: public Father
{
public:
    Son()  { cout << "son" << endl; }
    ~Son() {  cout << "~son" << endl; }
};

int main()
{
    Father *father = new Son();
    delete (Son *)father;
}
  • 结果
father
son
~son
~father
父类析构函数不实现,删除父指针
#include 
#include 
using namespace std;

class Father
{
public:
    Father() { cout << "father" << endl; }
    // virtual ~Father() {}
    // virtual ~Father() { cout << "~father" << endl; }
};


class Son: public Father
{
public:
    Son()  { cout << "son" << endl; }
    ~Son() {  cout << "~son" << endl; }
};

int main()
{
    Father *father = new Son();
    delete father;
}
  • 结果
father
son
父类析构函数不实现,父指针做子类型转换后再删除
#include 
#include 
using namespace std;

class Father
{
public:
    Father() { cout << "father" << endl; }
    // virtual ~Father() {}
    // virtual ~Father() { cout << "~father" << endl; }
};


class Son: public Father
{
public:
    Son()  { cout << "son" << endl; }
    ~Son() {  cout << "~son" << endl; }
};

int main()
{
    Father *father = new Son();
    delete (Son*)father;
}
  • 结果
father
son
~son
3 结构体

C++类就是对结构体的拓展,所以结构体和类的使用一致。

#include 
#include 
using namespace std;

struct Father
{
public:
    Father() { cout << "father" << endl; }
    // virtual ~Father() {}
    virtual ~Father() { cout << "~father" << endl; }
};


struct Son: public Father
{
public:
    Son()  { cout << "son" << endl; }
    ~Son() {  cout << "~son" << endl; }
};

int main()
{
    Father *father = new Son();
    delete father;
}
  • 结果
father
son
~son
~father
4 详解

2.7 多态与虚函数

参考

1、C++概述
2、C++多态的delete调用的是父类析构函数,那么子类对象的内存释放了吗?
3、delete基类指针释放子类对象
4、C++中虚函数和纯虚函数的区别

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号