Disappearance of C ++ destructor - achieve dynamic virtual destructor

You can create memory, using the class destructor to cast memory usage class method in the structure of C ++ class, but there is such a situation will lead to: release the memory even in the destructor, but the destructor is not called memory leaks result, the following code.

. 1 #include <the iostream>
 2 #include < String >
 . 3  
. 4  the using  namespace STD;
 . 5  
. 6  class Father
 . 7  {
 . 8  public :
 . 9      Father ( const  char * addr = " Father parameterized configuration " )
 10      {
 . 11          COUT << " performed the constructor Father " << endl;
 12 is          int len = strlen (addr) + . 1 ;
 13 is          the this -> addr = new new char [len];
 14          strcpy_s ( the this -> addr, len, addr);
 15      }
 16  
. 17      ~ Father ()
 18 is      {
 . 19          COUT << " performed destructors Father " << endl;
 20 is          IF ( the this - > addr)
 21 is          {
 22 is              Delete addr;
 23 is              addr = NULL;
 24          }
 25      }
 26 is  Private :
 27      char *addr;
 28  };
 29  
30  
31 is  class Son: public Father
 32  {
 33 is  public :
 34 is      Son ( const  char * Game = " Son parameterized structure " , const  char * addr = " Inheritance Father parameterized constructor " ): Father (addr )
 35      // Son (const char * = Game "Son parameterized constructor") 
36      {
 37 [          COUT << " performed Son constructor " << endl;
 38 is          intstrlen = len (Game) + . 1 ;
 39          the this -> Game = new new  char [len];
 40          strcpy_s ( the this -> Game, len, Game);
 41 is      }
 42 is      ~ Son ()
 43 is      {
 44 is          COUT << " performed Son destructor " << endl;
 45          IF ( the this -> Game)
 46 is          {
 47              Delete Game;
 48              Game = NULL;
 49          }
 50     }
51 
52 private:
53     char* game;
54 };
55 
56 int main()
57 {
58     cout << "--------case 1----------" <<endl;
59     Father* father = new Father();
60     delete father;
61 
62     cout << "\n--------case 2----------" << endl;
63     Son* son = new Son();
64     delete son;
65 
66     cout << "\n--------case 3----------" << endl;
67     father = new Son();
68     delete father;
69 
70     return 0;
71 }

 

operation result:

 

 

 More than print, case 1 and case 2 are normal. Look at the 67 lines of code, looking at the case of print 3, will find little to release a Son,

The solution to this problem is simple, tell the parent class destructor is virtual function can be modified. Or code above, line 17, before increasing the parent virtual destructor:

. 1 #include <the iostream>
 2 #include < String >
 . 3  
. 4  the using  namespace STD;
 . 5  
. 6  class Father
 . 7  {
 . 8  public :
 . 9      Father ( const  char * addr = " Father parameterized configuration " )
 10      {
 . 11          COUT << " performed the constructor Father " << endl;
 12 is          int len = strlen (addr) + . 1 ;
 13 is          the this -> addr = new new char [len];
 14          strcpy_s ( the this -> addr, len, addr);
 15      }
 16  
. 17     Virtual ~ Father ()
 18 is     {
 . 19          COUT << " performed destructors Father " << endl;
 20 is IF ( the this -> addr)
 21 is         {
 22 is Delete addr;
 23 is              addr = NULL;
 24         }
 25     }
 26 is Private :
 27 char                                 * Addr;
 28  };
 29  
30  
31 is  class Son: public Father
 32  {
 33 is  public :
 34 is      Son ( const  char * Game = " Son parameterized structure " , const  char * addr = " Inheritance Father parameterized constructor " ): Father ( addr)
 35      // Son (const char * = Game "Son parameterized constructor") 
36      {
 37 [          COUT << " performed Son constructor " << endl;
 38 is          intstrlen = len (Game) + . 1 ;
 39          the this -> Game = new new  char [len];
 40          strcpy_s ( the this -> Game, len, Game);
 41 is      }
 42 is      ~ Son ()
 43 is      {
 44 is          COUT << " performed Son destructor " << endl;
 45          IF ( the this -> Game)
 46 is          {
 47              Delete Game;
 48              Game = NULL;
 49          }
 50     }
51 
52 private:
53     char* game;
54 };
55 
56 int main()
57 {
58     cout << "--------case 1----------" <<endl;
59     Father* father = new Father();
60     delete father;
61 
62     cout << "\n--------case 2----------" << endl;
63     Son* son = new Son();
64     delete son;
65 
66     cout << "\n--------case 3----------" << endl;
67     father = new Son();
68     delete father;
69 
70     return 0;
71 }

 

Print Results:

Memory application are correct release.

 

 

Conclusion: When we define parent class destructor virtual, such modifications are not to overwrite, the pointer will he Father classes (class-yl) Delete operation is performed using the dynamic destructor (if this pointer is subclass object, it will first call the destructor of the subclass, then call their own class destructor).

Habit: In order to prevent a memory leak, it is preferable to add a keyword in the destructor virtual base class, the base class virtual function destructors.

 

 

 

 

 

 

 

 

======================================================================================================================

Guess you like

Origin www.cnblogs.com/CooCoChoco/p/12610015.html