当前位置:网站首页>谈谈为何需要将类的成员函数声明为private
谈谈为何需要将类的成员函数声明为private
2022-07-27 05:19:00 【Mr FF】
1 析构函数声明为私有, 有些资源必须要在析构前释放掉,则将析构函数声明为私有,然后另外再定义一个公有的destroy函数,先做释放资源操作,再调用析构函数。 delete this.
2 当类成员中有文件描述符,锁之类的系统资源时,因为这些资源不具备可复制性。所以要防止使用者复制使用它们。此时的做法是将copy构造函数、赋值操作符声明为private
3 析构函数声明为私有,则在创建该类的对象时必须是在堆上创建,如果是在栈上创建会导致编译不过。原因:编译器可见性,在栈上创建对象时必须是析构函数可见,而声明为private则不可见。
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
template<typename T>
class DeviceManager {
public:
DeviceManager(const T a, string filePath) : m_deviceNumber(a), m_filePath(filePath) {
}
void CreateDevice() {
m_file = fopen(m_filePath.c_str(), "w");
}
void DestroyDevice() {
if (m_file != nullptr) {
cout << "clean the resource" << endl;
fclose(m_file);
m_file = nullptr;
}
delete this;
}
private:
// private destructure.
virtual ~DeviceManager() {
cout << "~DeviceManager()" << endl;
}
// private assignment.
DeviceManager& operator=(const DeviceManager& deviceManager)
{
if (&deviceManager != this) {
m_deviceNumber = deviceManager.m_deviceNumber;
m_file = deviceManager.m_file;
m_filePath = deviceManager.m_filePath;
}
return *this;
}
// private copy constructure.
DeviceManager(const DeviceManager& deviceManager)
{
cout << "copy constructure" << endl;
m_deviceNumber = deviceManager.m_deviceNumber;
m_file = deviceManager.m_file;
m_filePath = deviceManager.m_filePath;
}
private:
string m_filePath;
T m_deviceNumber;
FILE *m_file;
};
int main()
{
string deviceNumber = "ssd";
//DeviceManager<string> deviceManager(deviceNumber, "~/my_project");
DeviceManager<string> *p = new DeviceManager<string>(deviceNumber, "~/my_project/aa.log");
p->CreateDevice();
p->DestroyDevice();
return 0;
}边栏推荐
- 维度问题以及等高线
- Day 11. Evidence for a mental health crisis in graduate education
- [Haowen planting grass] knowledge of root domain name - Ruan Yifeng's Weblog
- 代码随想录笔记_哈希_242有效的字母异位词
- Digital image processing Chapter 2 fundamentals of digital image
- [MVC Architecture] MVC model
- Day 9. Graduate survey: A love–hurt relationship
- 19. Up and down sampling and batchnorm
- Gbase 8C - SQL reference 6 SQL syntax (7)
- 贪心高性能神经网络与AI芯片应用研修
猜你喜欢

Digital image processing Chapter 2 fundamentals of digital image

【MVC架构】MVC模型
![[MySQL learning] 8](/img/25/84d5acbdd8aba3455ab8e3eb17dfa8.png)
[MySQL learning] 8

【好文种草】根域名的知识 - 阮一峰的网络日志

DSGAN退化网络

19.上下采样与BatchNorm

向量和矩阵的范数

视觉横向课题bug1:FileNotFoundError: Could not find module ‘MvCameraControl.dll‘ (or one of it

Digital image processing Chapter 4 - frequency domain filtering

11.感知机的梯度推导
随机推荐
服务器相关的指标解释
2. Simple regression problem
Matlab 画图(超详细)
Handler操作记录 Only one Looper may be created per thread
古老的艺术-用好长尾关键词
GBASE 8C——SQL参考6 sql语法(14)
1. Introduction to pytorch
Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
2021中大厂php+go面试题(2)
西瓜书学习第五章---神经网络
4.张量数据类型和创建Tensor
RK3288板卡HDMI显示uboot和kernel的logo图片
数字图像处理 第八章——图像压缩
17. Attenuation of momentum and learning rate
13. Logistic regression
GBASE 8C——SQL参考6 sql语法(7)
关于pytorch转onnx经常出现的问题
Day 9. Graduate survey: A love–hurt relationship
方差与协方差
新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic