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

  • 相关阅读:
    C协程使用举例
    教你理解复杂的CC++声明(转)
    C语言学习趣事_关于指针转换
    Howard's Startup Game @meditic » 降级论
    认识C++语言关键字和语法extern和双冒号
    Linux操作系统的种种集成开发环境
    新建Android项目时使用project from existing source导入已经存在的项目报 AndroidManifest.xml file missing错误解决方案
    JAVA中IP和整数相互转化
    自定义Struts2实现
    Web编程所需的必要知识、环境工具相关
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3442822.html
Copyright © 2011-2022 走看看