zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【分享】浅析Quora的技术架构 狼人:
    【观点】在苹果公司学到的编程技巧 狼人:
    【观点】工作效率上的错觉 狼人:
    提高编程技巧的十大方法 狼人:
    微软推出IE10第二个平台预览版 狼人:
    读取文件将 Excel 文件 转换成 CSV 文件 解决方案
    组合生成组合的生成之生成下一个组合 By ACReaper
    二叉树遍历二叉树的实现及先序、中序、后序遍历
    图片切换[置顶] 送大家几款可以运用到实际项目的flash+xml控件
    动画效果程序员的最高境界就是能够参加全球DEMO大赛
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3442822.html
Copyright © 2011-2022 走看看