C++中禁止在栈中实例化的类

编程入门 行业动态 更新时间:2024-10-24 21:22:17

C++中禁止在栈中<a href=https://www.elefans.com/category/jswz/34/1771375.html style=实例化的类"/>

C++中禁止在栈中实例化的类

C++中禁止在栈中实例化的类

栈空间通常有限。如果您要编写一个数据库类,其内部结构包含数 TB 数据,可能应该禁止在栈上实例化它,而只允许在自由存储区中创建其实例。为此,关键在于将析构函数声明为私有的:

class MonsterDB
{
private:~MonsterDB(); // private destructor//... members that consume a huge amount of data
};

通过声明私有的析构函数,可禁止像下面这样创建实例:

int main()
{MonsterDB myDatabase; // compile error// … more codereturn 0;
}

上述代码试图在栈上创建实例。退栈时,将弹出栈中的所有对象,因此编译器需要在 main() 末尾调用析构函数 ~MonsterDB(),但这个析构函数是私有的,即不可用,因此上述语句将导致编译错误。
将析构函数声明为私有的并不能禁止在堆中实例化:

int main()
{MonsterDB* myDatabase = new MonsterDB(); // no error// … more codereturn 0;
}

上述代码将导致内存泄露。由于在 main 中不能调用析构函数,因此也不能调用 delete。为了解决这种问题,需要在 MonsterDB 类中提供一个销毁实例的静态公有函数(作为类成员,它能够调用析构函数),如以下示例程序所示:

#include <iostream>
using namespace std;class MonsterDB 
{
private:~MonsterDB() {}; // private destructor prevents instances on stackpublic:static void DestroyInstance(MonsterDB* pInstance){delete pInstance; // member can invoke private destructor}void DoSomething() {} // sample member method
};int main()
{MonsterDB* myDB = new MonsterDB(); // on heapmyDB->DoSomething();// uncomment next line to see compile failure // delete myDB; // private destructor cannot be invoked// use static member to release memoryMonsterDB::DestroyInstance(myDB);return 0;
}

这些代码旨在演示如何创建禁止在栈中实例化的类。为此,关键是将构造函数声明成私有的,如第 6 行所示。为分配内存,第 9~12 行的静态函数 DestroyInstance() 必不可少,因为在 main() 中不能对 myDB 调用 delete。为了验证这一点,您可取消对第 23 行的注释。

数据库类把析构函数设置为私有,只能使用new在自由储存区中创建其对象。如下代码:

class MonsterDB
{
private:~MonsterDB( ) {};
public:static void DestroyInstance(MonsterDB* pInstance){delete pInstance;}//……imagine a few other methods
};int main()
{MonsterDB* pMyDatabase = new MonsterDB();MonsterDB :: DestroyInstance(pMyDatabase);return 0;
}

该文章会更新,欢迎大家批评指正。

推荐一个零声学院的C++服务器开发课程,个人觉得老师讲得不错,
分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,
fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,
TCP/IP,协程,DPDK等技术内容
点击立即学习:C/C++后台高级服务器课程

更多推荐

C++中禁止在栈中实例化的类

本文发布于:2023-11-16 11:39:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!