本文主要讨论auto_ptr、unique_ptr、shared_ptr这三种智能指针的区别与使用场景。
auto_ptr与unique_ptr:auto_ptr从c++11之后已停止使用,unique_ptr能够再auto_ptr的所有使用条件下运用,且能在编译阶段纠错。相比于auto_ptr,unique_ptr的优势如下:
不会出现重复删除的情况
unique_ptr在一般情况下是不能够作为“=”右边的元素进行赋值操作的,从而防止了对同一个对象重复删除而带来的错误,以下几种情况unique_ptr能够作为赋值号的右值:
1、unique_ptr
p2 = fun("aa"); unique_ptr
fun(string s){ unique_ptr
temp(new string(s)); return temp;
}
此时fun函数返回的unique_ptr智能指针,在完成赋值操作后会自动销毁。
2、unique_ptr
temp(new string(s)); unique_ptr
p2=move(temp); 在move操作下,系统会直接讲p2指向temp中的内容,通过让temp不再指向原来的内容,所以就不存在多次delete的情况。
3、unique_ptr
temp(new string(s)); shared_ptr
p2=temp; shared_ptr包含一个显示构造函数,可用于讲右值unique_ptr转换为shared_ptr
shared_ptr与unique_ptr:其主要区别在于该智能指针所指向的对象是否可以被多个变量引用
1、当程序要使用多个指向同一个对象的指针,应该选择shared_ptr
2、shared_ptr于unique_ptr使用的时候,都需要用new或new[]去分配内存。
shared_ptr:new
unique_ptr:new、new[]



