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的更多相关文章

  1. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  2. C++模拟C#事件委托机制(二)

    原文 来自于http://www.cnblogs.com/netssfy/archive/2010/02/02/1662056.html 为了解决非法地址访问的冲突,首先需要知道发生该错误的原因是什么 ...

  3. 私有析构函数 Android 代码分析

    有人说声明 Private Destructor, 这对象只能在 stack 上创建,不能在Heap上创建, 其实错了, 这样的程序编译都过不了. 那为何会有 Private Destructor, ...

  4. “Clang” CFE Internals Manual---中文版---"Clang"C语言前端内部手册

    原文地址:http://clang.llvm.org/docs/InternalsManual.html 译者:史宁宁(snsn1984) "Clang"C语言前端内部手册 简介 ...

  5. C++ Core Guidelines

    C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...

  6. 学习笔记之C++入门到精通(名师教学·手把手教会)【职坐标】_腾讯课堂

    C++入门到精通(名师教学·手把手教会)[职坐标]_腾讯课堂 https://ke.qq.com/course/101465#term_id=100105503 https://github.com/ ...

  7. 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 ...

  8. C++中public,protected,private派生类继承问题和访问权限问题

    C++中public,protected,private派生类继承问题和访问权限问题 当一个子类从父类继承时,父类的所有成员成为子类的成员,此时对父类成员的访问状态由继承时使用的继承限定符决定. 1. ...

  9. 构造函数constructor 与析构函数destructor(五)

    我们知道当调用默认拷贝构造函数时,一个对象对另一个对象初始化时,这时的赋值时逐成员赋值.这就是浅拷贝,当成员变量有指针时,浅拷贝就会在析构函数那里出现问题.例如下面的例子: //test.h #ifn ...

随机推荐

  1. Fiddler抓包工具简介:(二)下载安装及配置证书和代理

    Fiddler下载安装及配置 一.安装过程: 下载官网:https://www.telerik.com/fiddler 安装过程:一路next即可 启动Fiddler:当你启动了Fiddler,程序将 ...

  2. IDEA中Update resources和Update classes and resources、Redeploy、Restart server的区别

    选项 描述 update resources 所有更改的资源都会更新(HTML,JSP,JavaScript,CSS和图像文件) update classes and resources 更改的资源将 ...

  3. postman使用(待更新)

    参考文档

  4. flower 指定app

    flower 指定app # tasks.py from celery import Celery celery = Celery(broker="amqp://", backen ...

  5. linux磁盘空间查看

    du -h --max-depth=1 du -sh df -h

  6. 算法学习->整数拆分问题

    动态规划典型题目/ 00 题目 将正整数n无需拆分为最大数为k的拆分方案有多少种?​要求所有的拆分方案不重复. 示例: 输入:n=5,k=5 输出:(5,5)=7 示例分析: 5=5 5=4+1 5= ...

  7. InnoDB 索引详解

    1.什么是索引 索引是存储引擎用于快速找到记录的一种数据结构. 2.索引有哪些数据结构 顺序查找结构:这种查找效率很低,复杂度为O(n).大数据量的时候查询效率很低. 有序的数据排列:二分查找法又称折 ...

  8. tomcat的log日志乱码解决方案

    Intellij idea Tomcat输出log中文乱码 配置tomcat在VM options添加-Dfile.encoding=UTF8 -Dsun.jnu.encoding=UTF8 重启后控 ...

  9. git使用小技巧

    1. 合并一个分支的某次提交到另一个分支上 例如 将dev的某次提交 asfdiwehfsalkdnva872383 合并到master # git checkout master # git che ...

  10. Linux设置默认的声卡

    首先查看自己电脑上的声卡 使用命令行查看 orangepi@orangepi3:~$ ll /proc/asound/ total 0 dr-xr-xr-x 4 root root 0 Dec 23 ...