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

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

提交回复
热议问题