C++ Destructors with Vectors, Pointers,

后端 未结 7 2033
清酒与你
清酒与你 2020-12-15 02:57

As far as I know, I should destroy in destructors everything I created with new and close opened filestreams and other streams. However, I have some doubts abou

7条回答
  •  一个人的身影
    2020-12-15 03:16

    • if they are in automatic storage, yes. You can have std::string* s = new std::string, in which case you have to delete it yourself.

    • nothing, you need to manually delete memory you own (for memory allocated with new).

    • if you allocated b with new, you should destroy it in the destructor explicitly.

    A good rule of thumb is to use a delete/delete[] for each new/new[] you have in your code.

    A better rule of thumb is to use RAII, and use smart pointers instead of raw pointers.

提交回复
热议问题