当前位置:网站首页>谈谈为何需要将类的成员函数声明为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;
}边栏推荐
- 【好文种草】根域名的知识 - 阮一峰的网络日志
- 18. Convolutional neural network
- Digital image processing Chapter 5 - image restoration and reconstruction
- Emoji表情符号用于文本情感分析-Improving sentiment analysis accuracy with emoji embedding
- Day10. Work organization and mental health problems in PhD students
- 数字图像处理 第二章 数字图像基础
- 14. Example - Multi classification problem
- Day14. 用可解释机器学习方法鉴别肠结核和克罗恩病
- Brief analysis of application process creation process of activity
- Rk3288 board HDMI displays logo images of uboot and kernel
猜你喜欢

西瓜书第三章---线性模型学习笔记

方差与协方差

Day 6.重大医疗伤害事件网络舆情能量传播过程分析*———以“魏则西事件”为例

【12】理解电路:从电报机到门电路,我们如何做到“千里传信”?

A photo breaks through the face recognition system: you can nod your head and open your mouth, netizens

17. Attenuation of momentum and learning rate

7. Merger and division

3.分类问题---手写数字识别初体验

Matlab 画图(超详细)

关于pytorch反向传播的思考
随机推荐
16.过拟合欠拟合
13.逻辑回归
模型的推理速度
Gbase 8C - SQL reference 6 SQL syntax (5)
向量和矩阵的范数
GBASE 8C——SQL参考6 sql语法(12)
GBASE 8C——SQL参考6 sql语法(13)
GBASE 8C——SQL参考6 sql语法(7)
Day 8.Developing Simplified Chinese Psychological Linguistic Analysis Dictionary for Microblog
贪心高性能神经网络与AI芯片应用研修
19.上下采样与BatchNorm
图像超分辨率评价指标
Emoji表情符号用于文本情感分析-Improving sentiment analysis accuracy with emoji embedding
西瓜书第三章---线性模型学习笔记
pytorch中交叉熵损失函数的细节
pytorch使用data_prefetcher提升数据读取速度
[MVC Architecture] MVC model
【高并发】面试官
GBASE 8C——SQL参考4 字符集支持
Rk3288 board HDMI displays logo images of uboot and kernel