一、unique_ptr类
头文件:#include<memory>
智能指针,是一个模板。创建智能指针时,必须提供指针所指的类型
与shared_ptr的不同之处:
shared_ptr所指向的对象可以有多个其他shared_ptr智能指针
而unique_ptr所指向的对象只能有一个unique_ptr指针,也就是自己。当unique_ptr被销毁时,它所指向的对象也被销毁
二、unique_ptr类的初始化
unique_ptr指针需要绑定到一个new返回的指针上,并且不能直接将new的结果用赋值运算符“=”赋值给unique_ptr
unique_ptr<double> p1;//正确
unique_ptr<int> p2(new int(42));//正确
unique_ptr<int> p3 = new int(42);//错误
三、unique_ptr之间不存在拷贝与赋值
原因:因为unique_ptr所指向的对象只能有一个unique_ptr指针,也就是一个引用计数。因此unique_ptr不支持普通的拷贝和赋值操作
unique_ptr<string> p1(new string("HelloWorld"));
unique