Does delete on a pointer to a subclass call the base class destructor?

前端 未结 11 2136
面向向阳花
面向向阳花 2020-11-27 09:16

I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class (clas

相关标签:
11条回答
  • 2020-11-27 09:29

    You should delete A yourself in the destructor of B.

    0 讨论(0)
  • 2020-11-27 09:30

    It is named "destructor", not "deconstructor".

    Inside the destructor of each class, you have to delete all other member variables that have been allocated with new.

    edit: To clarify:

    Say you have

    struct A {}
    
    class B {
        A *a;
    public:
        B () : a (new A) {}
        ~B() { delete a; }
    };
    
    class C {
        A *a;
    public:
        C () : a (new A) {}        
    };
    
    int main () {
        delete new B;
        delete new C;
    }
    

    Allocating an instance of B and then deleting is clean, because what B allocates internally will also be deleted in the destructor.

    But instances of class C will leak memory, because it allocates an instance of A which it does not release (in this case C does not even have a destructor).

    0 讨论(0)
  • 2020-11-27 09:30

    If you have a usual pointer (A*) then the destructor will not be called (and memory for A instance will not be freed either) unless you do delete explicitly in B's destructor. If you want automatic destruction look at smart pointers like auto_ptr.

    0 讨论(0)
  • 2020-11-27 09:32

    No. the pointer will be deleted. You should call the delete on A explicit in the destructor of B.

    0 讨论(0)
  • 2020-11-27 09:32

    no it will not call destructor for class A, you should call it explicitly (like PoweRoy told), delete line 'delete ptr;' in example to compare ...

      #include <iostream>
    
      class A
      {
         public:
            A(){};
            ~A();
      };
    
      A::~A()
      {
         std::cout << "Destructor of A" << std::endl;
      }
    
      class B
      {
         public:
            B(){ptr = new A();};
            ~B();
         private:
            A* ptr;
      };
    
      B::~B()
      {
         delete ptr;
         std::cout << "Destructor of B" << std::endl;
      }
    
      int main()
      {
         B* b = new B();
         delete b;
         return 0;
      }
    
    0 讨论(0)
  • 2020-11-27 09:36
    class B
    {
    public:
        B()
        {
           p = new int[1024];  
        }
        virtual ~B()
        {
            cout<<"B destructor"<<endl;
            //p will not be deleted EVER unless you do it manually.
        }
        int *p;
    };
    
    
    class D : public B
    {
    public:
        virtual ~D()
        {
            cout<<"D destructor"<<endl;
        }
    };
    

    When you do:

    B *pD = new D();
    delete pD;
    

    The destructor will be called only if your base class has the virtual keyword.

    Then if you did not have a virtual destructor only ~B() would be called. But since you have a virtual destructor, first ~D() will be called, then ~B().

    No members of B or D allocated on the heap will be deallocated unless you explicitly delete them. And deleting them will call their destructor as well.

    0 讨论(0)
提交回复
热议问题