Predict the output of following programs.

  1. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class Test
  5. 5 {
  6. 6 private:
  7. 7 ~Test()
  8. 8 {
  9. 9 }
  10. 10 };
  11. 11 int main()
  12. 12 {
  13. 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. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class Test
  5. 5 {
  6. 6 private:
  7. 7 ~Test()
  8. 8 {
  9. 9 }
  10. 10 };
  11. 11 int main()
  12. 12 {
  13. 13 Test t;
  14. 14 return 0;
  15. 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. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class Test
  5. 5 {
  6. 6 private:
  7. 7 ~Test() {}
  8. 8 };
  9. 9 int main()
  10. 10 {
  11. 11 Test *t;
  12. 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. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class Test
  5. 5 {
  6. 6 private:
  7. 7 ~Test() {}
  8. 8 };
  9. 9 int main()
  10. 10 {
  11. 11 Test *t = new Test;
  12. 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. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class Test
  5. 5 {
  6. 6 private:
  7. 7 ~Test() {}
  8. 8 };
  9. 9 int main()
  10. 10 {
  11. 11 Test *t = new Test;
  12. 12 delete t;
  13. 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. 1 #include <iostream>
  2. 2
  3. 3 // A class with private destuctor
  4. 4 class Test
  5. 5 {
  6. 6 private:
  7. 7 ~Test() {}
  8. 8 friend void destructTest(Test* );
  9. 9 };
  10. 10
  11. 11 // Only this function can destruct objects of Test
  12. 12 void destructTest(Test* ptr)
  13. 13 {
  14. 14 delete ptr;
  15. 15 }
  16. 16
  17. 17 int main()
  18. 18 {
  19. 19 // create an object
  20. 20 Test *ptr = new Test;
  21. 21
  22. 22 // destruct the object
  23. 23 destructTest (ptr);
  24. 24
  25. 25 return 0;
  26. 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. SQL注入之猫舍

    第一步:先查看是否存在注入点:构造?id=1 and 1=1 回车后发现页面正常 构造?id=1 and 1=2 发现页面异常,得出结论:存在注入点 第二步:判断字段数 当输入order by 1和o ...

  2. Jackson & fastJson的使用

    Jackson import lombok.Data; @Data public class Student { private Long id; private String name; priva ...

  3. 【JavaScript定时器小案例】常见的几种定时器实现的案例

    [JavaScript定时器小案例]常见的几种定时器实现的案例 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 在日常开发 ...

  4. python递归三战:Sierpinski Triangle、Tower of Hanoi、Maze Exploring

    本文已做成视频教程投稿b站(视频版相对文本版有一些改进),点击观看视频教程 本文主要通过三个实例来帮助大家理解递归(其展示动画已上传B站): 谢尔宾斯基三角形(Sierpinski Triangle) ...

  5. react之路由

    功能:让用户从一个视图(组件)导航到另一个视图(组件) 前端路由是一套映射规则,在React中,是URL路径与组件的对应关系 使用React路由简单来说,就是配置路径和组件 路由的使用 1.安装路由 ...

  6. [at4631]Jewels

    如果要选某颜色,必然会选该颜色最大的两个,那么不妨将这两个宝石权值修改为两者的平均数,显然不影响两者的和,也即不影响答案 接下来,将所有宝石按权值从大到小排序,并在权值相同时按颜色编号从小到大(使颜色 ...

  7. [cf582E]Boolean Function

    由于每一个运算都有括号,因此添加的运算不会改变运算顺序 先将其建出一棵表达式树,也就是维护两个栈,是节点和运算符优先级单调递增的栈(设置左括号优先级最低,右括号弹出直至左括号) 每一次运算,也就是新建 ...

  8. [luogu5426]Balancing Inversions

    由于交换是相邻交换,所以分为两类:1.左右区间内部交换,那么一定会让逆序对数量$\pm 1$,也就是说如果没有左右区间之间交换,那么答案就是$|ansL-ansR|$(ans表示逆序对数量)2.左右区 ...

  9. 如何解决 ASP.NET Core 中的依赖问题

    依赖性注入是一种技术,它允许我们注入一个特定类的依赖对象,而不是直接创建这些实例. 使用依赖注入的好处显而易见,它通过放松模块间的耦合,来增强系统的可维护性和可测试性. 依赖注入允许我们修改具体实现, ...

  10. 状压DP详解+题目

    介绍 状压dp其实就是将状态压缩成2进制来保存 其特征就是看起来有点像搜索,每个格子的状态只有1或0 ,是另一类非常典型的动态规划 举个例子:有一个大小为n*n的农田,我们可以在任意处种田,现在来描述 ...