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 ...
随机推荐
- MySQL之DDL数据定义语言:库、表的管理
库的管理 常用命令 #创建库 create database if not exists 库名 [ character set 字符集名]; create database if not exists ...
- Java try catch语句块中try()的括号中代码作用
了解过Mybatis,都知道DefacltSqlSession是线程不安全的.每次执行查询都需要新建一个sqlSession.因此官方给的建议写法如下: Mybatis3 从 SqlSessionFa ...
- yum install hadoop related client
yum list avaliable hadoop\* yum list installed yum repolist repo is in /etc/yum.repos.d yum install ...
- RocketMQ源码详解 | Consumer篇 · 其一:消息的 Pull 和 Push
概述 当消息被存储后,消费者就会将其消费. 这句话简要的概述了一条消息的最总去向,也引出了本文将讨论的问题: 消息什么时候才对被消费者可见? 是在 page cache 中吗?还是在落盘后?还是像 K ...
- 菜鸡的Java笔记 图书馆
图书大厦 开发要求 现在要求模拟一个图书大厦图书管理的程序结构,可以在图书大厦实现某一类图书的上架操作,下架操作,以及关键字模糊查询的操作 注 ...
- PAT A1060——string的常见用法详解
string 常用函数实例 (1)operator += 可以将两个string直接拼接起来 (2)compare operator 可以直接使用==.!=.<.<=.>.>= ...
- [bzoj4777]Switch Grass
结论:最短路径一定是单独的一条边且在最小生成树上,可以用反证法证明.那么求出最小生成树,对于每一个点建立一棵权值线段树,再对每一个权值线段树上的叶子节点开一个multiset,维护所有儿子中该种颜色的 ...
- IPv4 寻址方式简介
IPv4 支持三种不同类型的寻址模式.单播寻址方式.广播寻址方式和组播寻址方式.本章节我们来介绍这些寻址方式. 单播寻址方式 在这种模式下,数据只发送到一个目标主机.Destination Addre ...
- 最强最全面的Hive SQL开发指南,超四万字全面解析
本文整体分为两部分,第一部分是简写,如果能看懂会用,就直接从此部分查,方便快捷,如果不是很理解此SQL的用法,则查看第二部分,是详细说明,当然第二部分语句也会更全一些! 第一部分: hive模糊搜索表 ...
- PHP非对称加密-RSA
对称加密算法是在加密和解密时使用同一个密钥.与对称加密算法不同,非对称加密算法需要两个密钥--公开密钥(public key)和私有密钥(private key)进行加密和解密.公钥和密钥是一对,如果 ...