how much does the default destructor do

后端 未结 5 2244
死守一世寂寞
死守一世寂寞 2021-02-14 04:11

Does the default destructor in C++ classes automatically delete members that are not explicitly allocated in code? For example:

class C {
  public:
    C() {}
           


        
5条回答
  •  不要未来只要你来
    2021-02-14 04:56

    The constructor (in the absence of any ctor-initializer-list) calls the default constructor for each subobject.

    Since you have no base classes and your member variables are primitive types, it will do nothing at all.

    Same with the destructor. Yours is implicitly compiler-generated since you haven't declared one, and it will call the destructor for each subobject. Again that's trivial because your only subobject is an aggregate of primitives.

    Now, all memory of the class will be freed when you delete it. Since the array is embedded inside the class, it's part of the same memory region and will be freed at the same time.

提交回复
热议问题