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

  • 相关阅读:
    在Windows环境下使用docker
    C# 8.0中的模式匹配
    C# 8.0的新的using语法——Using declarations
    任务调度框架FluentScheduler简介
    任务调度框架Hangfire 简介
    Linux下的sqlserver简单试用
    使用Puppeteer进行数据抓取(五)——快速调试
    使用ptrace向已运行进程中注入.so并执行相关函数(转)
    ARM汇编编程基础之一 —— 寄存器
    ARM 汇编与C调用的若干问题(一般函数调用情况)
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3442822.html
Copyright © 2011-2022 走看看