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 ...
随机推荐
- IDEA Dao层快速跳转Mapper.xml 文件的插件
1.Idea 窗口→File→Setting→Plugins, 2.搜索 Free MyBatis plugin, install,等待安装完成后,Restart IDEA .
- SpringBoot Actuator — 埋点和监控
项目中看到了有埋点监控.报表.日志分析,有点兴趣想慢慢捣鼓一下 1. 数据埋点 监控机器环境的性能和业务流程或逻辑等各项数据,并根据这些数据生成对应的指标,那么我们就称为数据埋点.比如我们想知道某个接 ...
- Java学习(十六)
今天先学了文本标签 <p> <strong>永远不要相信诺克萨斯人的血条!</strong><!--表示一段内容的重要性--> <br /> ...
- 说透 Docker:虚拟化
本章内容将讲解 Docker 虚拟化.虚拟化本质.namespace.cgroups. Docker 虚拟化 关于Docker 本小节将介绍 Docker 虚拟化的一些特点. Docker 是一个开放 ...
- Prometheus+Grafana监控Kubernetes
涉及文件下载地址:链接:https://pan.baidu.com/s/18XHK7ex_J0rzTtfW-QA2eA 密码:0qn6 文件中需要下载的镜像需要自己提前下载好,eg:prom/node ...
- [hdu7044]Fall with Fake Problem
二分$T$和$S$第一个不同的位置,即需要对于$s$,判定是否存在$T[1,s]=S[1,s]$且满足条件的$T$ (注:这里的 ...
- [loj3330]猜数游戏
如果两个数$a_{x}$和$a_{y}$,$\exists 0<i,a_{x}^{i}\equiv a_{y}(mod\ p^{k})$,就建一条$x$到$y$的有向边,再对这张图强连通分量缩点 ...
- Dapr初体验之服务调用
初次理解服务调用 在微服务中,有一个难点就是:如果你想使用各个服务组件,你就得知道不同服务的地址和端口,也就是服务发现. 在传统应用我们是怎么做的?就是在web项目里配置上api地址,如下: 在一个w ...
- Vue3学习与实战 · 全局挂载使用Axios
在vue2中会习惯性的把axios挂载到全局,以方便在各个组件或页面中使用this.$http请求接口.但是在vue3中取消了Vue.prototype,在全局挂载方法和属性时,需要使用官方提供的gl ...
- IPv4 寻址方式简介
IPv4 支持三种不同类型的寻址模式.单播寻址方式.广播寻址方式和组播寻址方式.本章节我们来介绍这些寻址方式. 单播寻址方式 在这种模式下,数据只发送到一个目标主机.Destination Addre ...