Destructor not being called when leaving scope

后端 未结 8 525
离开以前
离开以前 2021-01-03 11:50

I am learning memory management in C++ and I don\'t get the why only some of the destructors are called when leaving scope. In the code below, only obj1 destructor is called

8条回答
  •  臣服心动
    2021-01-03 12:29

    Destructors are called automatically when an object that was created on the stack goes out of scope.

    With dynamically allocated objects, you need to call delete obj. delete will automatically call your destructor for you.

提交回复
热议问题