下面就让小编来带大家学习“C++中使用unique_ptr或者shared_ptr有什么作用”吧! C.149:使用unique_ptr或者shared_ptr避免忘记销毁使用new创建的对象 Reason(原因) 避免资源泄露。 Example(示例) voiduse(inti){ auto p = newint{7};//bad: initializelocalpointers with
If you first make an object and then give it to a shared_ptr constructor, you (most likely) ...
base = make_unique(); // use base locally } // destroy base Enforcement(实施建议) (简单)如果函数使用shared_ptr管理其内局部分配的对象,但是从来没有返回该智能指针或者将其传递个一个需要shared_ptr&的函数,发出警告。建议使用unique_ptr。 “C++什么时候使用unique_ptr而不是shared_ptr”的内容就介绍到...