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

  • 相关阅读:
    WORD2003电子签名插件(支持手写、签章)
    苹果App部署HTTPS进行在线下载安装
    解决安卓SDK更新连不通问题
    部署rfc5766-turn-server--谷歌推荐的开源穿透服务器 [复制链接]
    实测可用的免费STUN服务器!
    seajs加载jquery时提示$ is not a function该怎么解决
    tomcat发布web service教程
    Java WebService 简单实例
    HTML 5 断点续上传
    MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3442822.html
Copyright © 2011-2022 走看看