• 多态之虚析构和纯虚析构(当子类中有指针堆内存需要释放时,使用虚析构解决父类指针无法释放子类堆内存的问题,防止堆内存泄露)


    当子类中有指针堆内存需要释放时,使用虚析构解决父类指针无法释放子类堆内存的问题,防止堆内存泄露

     1 #include <iostream>
     2 #include "string"
     3 using namespace std;
     4 
     5 class Base
     6 {
     7 public:
     8      Base()
     9     {
    10          cout << "父类构造函数" << endl;
    11     }
    12      virtual~Base()//当子类中有指针堆内存需要释放时使用虚析构
    13      {
    14          cout << "父类析构函数" << endl;
    15      }
    16 
    17     virtual void func() = 0;
    18 };
    19 
    20 class Son :public Base
    21 {
    22 public:
    23     Son(string name)
    24     {  
    25         m_Name = new string(name);
    26 
    27         cout << "子类构造函数" << endl;
    28     }
    29     ~Son()
    30     {
    31         if (m_Name!=NULL)//释放堆内存的好习惯
    32         {
    33             delete m_Name;
    34             m_Name = NULL;
    35             cout << "子类析构函数" << endl;
    36         }
    37     }
    38 
    39     void func()
    40     {
    41         cout << *m_Name << "子类重写方法" << endl;
    42     }
    43 
    44     string  *m_Name;
    45 };
    46 
    47 void test(Base *base)
    48 { 
    49     base->func();
    50     base->~Base();
    51 }
    52 
    53 int main()
    54 {
    55     test(new Son("tom"));
    56 
    57     system("pause");
    58     return 0;
    59 }

    运行结果:

    2.纯虚析构:与虚析构作用一样,但是纯虚析构函数需要在类外实现函数体,否则无法运行,

    注意:只要包含一个纯虚函数的类都是抽象类,无法实例化

     1 #include <iostream>
     2 #include "string"
     3 using namespace std;
     4 
     5 class Base
     6 {
     7 public:
     8      Base()
     9     {
    10          cout << "父类构造函数" << endl;
    11     }
    12      //virtual~Base()//当子类中有指针堆内存需要释放时使用虚析构
    13      //{
    14         // cout << "父类析构函数" << endl;
    15      //}
    16      virtual~Base() = 0;//当子类中有指针堆内存需要释放时使用虚析构
    17     virtual void func() = 0;
    18 };
    19 Base::~Base()
    20 {
    21     cout << "父类析构函数" << endl;
    22 }
    23 
    24 class Son :public Base
    25 {
    26 public:
    27     Son(string name)
    28     {  
    29         m_Name = new string(name);
    30 
    31         cout << "子类构造函数" << endl;
    32     }
    33     ~Son()
    34     {
    35         if (m_Name!=NULL)//释放堆内存的好习惯
    36         {
    37             delete m_Name;
    38             m_Name = NULL;
    39             cout << "子类析构函数" << endl;
    40         }
    41     }
    42 
    43     void func()
    44     {
    45         cout << *m_Name << "子类重写方法" << endl;
    46     }
    47 
    48     string  *m_Name;
    49 };
    50 
    51 void test(Base *base)
    52 { 
    53     base->func();
    54     base->~Base();
    55 }
    56 
    57 int main()
    58 {
    59     test(new Son(" tom "));
    60 
    61     system("pause");
    62     return 0;
    63 }

    运行结果

  • 相关阅读:
    Shell Sort
    Insertion Sort
    Notations
    Nakamori Akina
    QuickSort
    Onedrive File Open Problem
    JSON Introduction
    System Call
    进程软中断通信
    Bubble Sort
  • 原文地址:https://www.cnblogs.com/rtblogs/p/12028561.html
Copyright © 2020-2023  润新知