当前位置: 首页 > news >正文

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++后台高级服务器课程

http://www.lryc.cn/news/219299.html

相关文章:

  • MsgPack和Protobuf
  • 自定义类型联合体
  • 【Shell 系列教程】Shell printf 命令( 六)
  • 2022年电工杯数学建模B题5G网络环境下应急物资配送问题求解全过程论文及程序
  • git reflog 恢复git reset --hard 回退的内容
  • kali Linux中更换为阿里镜像源
  • 【每日一题】移除链表元素(C语言)
  • stm32 ADC
  • linux网络服务综合项目
  • 每日一题(LeetCode)----数组--移除元素(三)
  • AI:57-基于机器学习的番茄叶部病害图像识别
  • 人工智能-深度学习计算:层和块
  • Linux第一个小程序进度条
  • JavaEE平台技术——预备知识(Maven、Docker)
  • 【ChatOCR】OCR+LLM定制化关键信息抽取(附开源大语言模型汇总整理)
  • 【位运算】XOR Construction—CF1895D
  • 解决Visual Studio Code 控制台中文乱码问题
  • React Native自学笔记
  • 程序员笔记本电脑选 windows 还是 MAC
  • 蓝桥杯每日一题2023.11.5
  • 多媒体应用设计师 2023年(含答案回忆版)
  • [Machine Learning][Part 8]神经网络的学习训练过程
  • Git 内容学习
  • Zookeeper3.7.1分布式安装部署
  • CSS必学:元素之间的空白与行内块的幽灵空白问题
  • C++类中对构造函数的重载
  • QtC++与QLabel详解
  • 090基于web+springboot的中小企业设备管理系统
  • input 调起键盘 ,键盘距离输入框底部太近
  • 前端深拷贝与浅拷贝的实现