Destructor not being called when leaving scope

后端 未结 8 472
离开以前
离开以前 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:38

    First of all you should use delete operator to destrory an object and not call its destructor directtly. Secondly, by doing new you are telling the compiler that you dont want to delete the object when it goes out of the scope. In such case you need to explictly fo delete objj2; to delete the object.

提交回复
热议问题