Private Destructor
Predict the output of following programs.
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test()
8 {
9 }
10 };
11 int main()
12 {
13 }
The above program compiles and runs fine. It is not compiler error to create private destructors.
What do you say about below program.
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test()
8 {
9 }
10 };
11 int main()
12 {
13 Test t;
14 return 0;
15 }
The above program fails in compilation.
The compiler notices that the local variable ‘t’ cannot be destructed because the destructor is private.
What about the below program?
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test() {}
8 };
9 int main()
10 {
11 Test *t;
12 }
The above program works fine. There is no object being constructed, the program just creates a pointer of type “Test *”, so nothing is destructed.
What about the below program?
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test() {}
8 };
9 int main()
10 {
11 Test *t = new Test;
12 }
The above program also works fine. When something is created using dynamic memory allocation, it is programmer’s responsibility to delete it. So compiler doesn’t bother.【没有delete t,所以不会调用private 析构函数,因此不会出错哦】
The below program fails in compilation. When we call delete, desturctor is called.
1 #include <iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 ~Test() {}
8 };
9 int main()
10 {
11 Test *t = new Test;
12 delete t;
13 }
We noticed in the above programs, when a class has private destructur, only dynamic objects of that class can be created.
Following is a way to create classes with private destructors and have a function as friend of the class. The function can only delete the objects.
1 #include <iostream>
2
3 // A class with private destuctor
4 class Test
5 {
6 private:
7 ~Test() {}
8 friend void destructTest(Test* );
9 };
10
11 // Only this function can destruct objects of Test
12 void destructTest(Test* ptr)
13 {
14 delete ptr;
15 }
16
17 int main()
18 {
19 // create an object
20 Test *ptr = new Test;
21
22 // destruct the object
23 destructTest (ptr);
24
25 return 0;
26 }
What is the use of private destructor?
Whenever we want to control destruction of objects of a class, we make the destructor private. For dynamically created objects, it may happen that you pass a pointer to the object to a function and the function deletes the object. If the object is referred after the function call, the reference will become dangling. See this for more details
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 10:48:14
Private Destructor的更多相关文章
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
- C++模拟C#事件委托机制(二)
原文 来自于http://www.cnblogs.com/netssfy/archive/2010/02/02/1662056.html 为了解决非法地址访问的冲突,首先需要知道发生该错误的原因是什么 ...
- 私有析构函数 Android 代码分析
有人说声明 Private Destructor, 这对象只能在 stack 上创建,不能在Heap上创建, 其实错了, 这样的程序编译都过不了. 那为何会有 Private Destructor, ...
- “Clang” CFE Internals Manual---中文版---"Clang"C语言前端内部手册
原文地址:http://clang.llvm.org/docs/InternalsManual.html 译者:史宁宁(snsn1984) "Clang"C语言前端内部手册 简介 ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- 学习笔记之C++入门到精通(名师教学·手把手教会)【职坐标】_腾讯课堂
C++入门到精通(名师教学·手把手教会)[职坐标]_腾讯课堂 https://ke.qq.com/course/101465#term_id=100105503 https://github.com/ ...
- C++ Knowledge series Inheritance & RTTI & Exception Handling
Inheritance The pointer or reference to base class can address/be assigned with any of the classes d ...
- C++中public,protected,private派生类继承问题和访问权限问题
C++中public,protected,private派生类继承问题和访问权限问题 当一个子类从父类继承时,父类的所有成员成为子类的成员,此时对父类成员的访问状态由继承时使用的继承限定符决定. 1. ...
- 构造函数constructor 与析构函数destructor(五)
我们知道当调用默认拷贝构造函数时,一个对象对另一个对象初始化时,这时的赋值时逐成员赋值.这就是浅拷贝,当成员变量有指针时,浅拷贝就会在析构函数那里出现问题.例如下面的例子: //test.h #ifn ...
随机推荐
- k8s入坑之路(13)服务迁移(定时任务 微服务 传统服务)
定时任务迁移kubernetes 服务迁移步骤 1.安装好java 2.安装好maven 项目打包 mvn package 测试传参运行 java -cp cronjob-demo-1.0-SNAPS ...
- k8s入坑之路(2)kubernetes架构详解
每个微服务通过 Docker 进行发布,随着业务的发展,系统中遍布着各种各样的容器.于是,容器的资源调度,部署运行,扩容缩容就是我们要面临的问题. 基于 Kubernetes 作为容器集群的管理平 ...
- Mysql基础教程:(七)MySQL基础练习
MySQL基础练习 一.创建student和score表 CREATE TABLE student (id INT(10) NOT NULL PRIMARY KEY ,name VARCHAR(20) ...
- go的变量、常量以及判断变量的数据类型
1.定义变量 p.p1 { margin: 0; font: 12px "Helvetica Neue"; color: rgba(69, 69, 69, 1) } span.s1 ...
- 基于霸道秉火的STM32F103ZET6嵌入式开发之------基于定时TIM3的PWM实验
1:PWM脉冲宽度调制 STM32 的定时器除了 TIM6 和 7.其他的定时器都可以用来产生 PWM 输出.其中高级定时器 TIM1 和 TIM8 可以同时产生多达 7 路的 PWM 输出.而通用定 ...
- django前后端分离403 csrf token missing or incorrect
根据这个链接https://stackoverflow.com/a/26639895 这是一个django的跨域访问问题. django,会对合法的跨域访问做这样的检验,cookies里面存储的'cs ...
- Python知识整理(三)
三.函数式编程与模块 1.函数式编程 1.高阶函数 把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式. 1.map/reduce map()函数接收两个参数,一个是函 ...
- 使用.NET5、Blazor和Electron.NET构建跨平台桌面应用
Electron.NET是一个嵌入了ASP.NET Core的Electron的封装,通过Electron.NET可以构建基于.NET5的跨平台的桌面应用,使得开发人员只需要使用ASP.NET Cor ...
- 论文翻译:2020_Densely connected neural network with dilated convolutions for real-time speech enhancement in the time domain
提出了模型和损失函数 论文名称:扩展卷积密集连接神经网络用于时域实时语音增强 论文代码:https://github.com/ashutosh620/DDAEC 引用:Pandey A, Wang D ...
- 不用Spring Boot的痛苦是什么?用了Spring Boot以后的好处是什么?
1.不用Spring Boot的痛苦是什么? (1)各种技术整合在一起,版本混乱,大量依赖自己去找,依赖冲突 (2)基于xml格式的配置文件,对各种技术框架进行大量的繁琐配置,mvc-servlet. ...