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

前端 未结 11 2201
面向向阳花
面向向阳花 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:50

    When you call delete on a pointer allocated by new, the destructor of the object pointed to will be called.

    A * p = new A;
    
    delete p;    // A:~A() called for you on obkect pointed to by p
    

提交回复
热议问题