使用智能指针的注意事项

本文详细介绍了C++智能指针的使用注意事项,包括避免使用`auto_ptr`,优先选择`unique_ptr`,谨慎处理裸指针,避免使用静态分配对象初始化智能指针,以及正确使用`make_unique`和`make_shared`等。同时,文章提醒开发者要关注智能指针的生命周期管理,防止循环引用和线程安全性问题。
摘要由CSDN通过智能技术生成
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_ptrunique_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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值