3.智能指针之unique_ptr
3.智能指针之unique_ptr 一、unique_ptrauto_ptr void fun() { int *a = new int(1000); //do something delete a; } return c++ unique _ptr unique_ptr“exclusive ownership”(专属所有权)设计的,专属所有权就是确保了在同一时间一个指针拥有一个对象的全部资源。 unique_ptrp++ void fun() { unique_ptr<int> a(new int(1000)); //do something } unique_ptr unique_ptr<string>p1 = new string(“string”);//错误 unique_ptr<int>p2(new int(100)); unique_ptr std::unique_ptr<std::string>p;//p p = nullptr; p.reset(); p p 我们也可以这样去检测: release() unique_ptr std::string *p2 = p.release();//p == nullptr unique_ptr unique_ptr exclusive ownership std::string* sp = new std::string(