1. 使用unique_ptr
以替代auto_ptr
auto_ptr
是C++98标准库提供的一个智能指针,但已被C++11明确声明不再支持。auto_ptr
具有以下缺陷:
* auto_ptr
有拷贝语义,拷贝后源对象变得无效,这可能引发很严重的问题;而unique_ptr
则无拷贝语义,但提供了移动语义,这样的错误不再可能发生,因为很明显必须使用std::move()
进行转移。
#include <iostream>
#include <memory>
using namespace std;
class A{
public:
string id;
A(string id):id(id){
cout<<id<<":构造函数"<<endl;}
~A(){
cout<<id<<":析构函数"<<endl;}
};
int main() {
auto_ptr<A> auto_ap(new A("auto_ptr")),auto_bp;
cout<<auto_ap.get()<<endl;
auto_bp = auto_ap;
cout<<auto_ap.get()<<endl;
unique_ptr<A> unique_ap(new A("unique_ptr")),unique_bp;
cout<<unique_ap.get()<<endl;
// unique_bp = unique_ap; // 报错
unique_bp = move(unique_ap);
cout<<unique_ap.get()<<endl;
return 0;
}
运行结果:
auto_ptr:构造函数
0x6115d0
0
unique_ptr:构造函数
0x6115f0
0
unique_ptr:析构函数
auto_ptr:析构函数
auto_ptr
不可作为容器元素,unique_ptr
可以作为容器元素。因为auto_ptr
的拷贝和赋值具有破坏性,不满足容器要求:拷贝或赋值后,两个对象必须具有相同值。auto_ptr
不可指向动态数组,unique_ptr
可以指向动态数组。因为unique_ptr
有unique_ptr<T[]>
重载版本,销毁动态对象时调用delete[]
。
#include <iostream>
#include <memory>
using namespace std;
class A{
public:
string id;
A(string id):id(id){
cout<<id<<":构造函数"<<endl;}
~A(){
cout<<id<<":析构函数"<<endl;}
};
int main() {
// auto_ptr<A[]> auto_ap(new A[1]{A("unique_ptr")}); // 报错
unique_ptr<A[]> unique_ap(new A[1]{A("unique_ptr")});
return 0;
}
运行结果:
unique_ptr:构造函数
unique_ptr:析构函数
auto_ptr
不可以自定义删除器deleter
,而unique_ptr
可以。
#include <iostream>
#include <memory>
using namespace std;
class A{
public:
string id;
A(string id):id(id){
cout<<id<<":构造函数"<<endl;}
~A(){
cout<<id<<":析构函数"<<endl;}
};
int main() {
unique_ptr<A,void(*)(A*)> unique_ap(new A[2]{A("unique_ptr0"),A("unique_ptr1")},
[](A *a){
delete []a;
});
return 0;
}
运行结果:
unique_ptr0:构造函数
unique_ptr1:构造函数
unique_ptr1:析构函数
unique_ptr0:析构函数
2. 尽量使用unique_ptr
而非shared_ptr
默认情况下,应使用unique_ptr
,理由如下:
- 若使用
unique_ptr
,当需要共享对象所有权时,依然可以将其转化为shared_ptr
,但反过来则不行。 - 使用
shared_ptr
需要消耗更多的资源,shared_ptr
需要维护一个指向动态内存对象的线程安全的引用计数器以及背后的一个控制块,这使它比unique_ptr
更加复杂。 - 共享对象所有权也许并非你的本意,但使用
shared_ptr
有可能造成其他程序员无意间通过赋值给另一个共享指针而修改了你共享出来的对象。
#include <iostream>
#include <memory>
using namespace std;
class A{
public:
string id;
A(string id):id(id){
cout<<id<<":构造函数"<<endl;}
~A(){
cout<<id<<":析构函数"<<endl;}
};
int main() {
unique_ptr<A> a(new