C++ Smart Pointer 智能指针

Smart Pointer 智能指针

C++作为语言层面也提供了,相应的解决方案,即智能指针,auto_ptr。虽然
auto_ptr 己经 deserted 了(引自 Google C++ 编程规范),它的后继者,诸如
share_ptr, weak_ptr 灵感均取自于此。

RAII Theory

RAII(Resource Acquisition Is Initialization),也称为"资源获取即初始
化",是 C++语言的一种管理资源、避免泄漏的惯用法。

C++标准保证任何情况下,已构造的对象最终会销毁,即它的析构函数最终会被调
用。
简单的说,RAII 的做法是使用一个对象,在其构造时获取资源,在对象生命期控
制对资源的访问使之始终保持有效,最后在对象析构的时候释放资源。

即 new 获取资源,constructor 构造资源 初始化。

auto_ptr

#include <iostream>
#include <memory>
using namespace std;
class A
{
public:
    A(){
        cout<<"A()"<<endl;
    }
    ~A(){
        cout<<"~A()"<<endl;
    }
    void func(){
        cout<<"hahaha"<<endl;
    }
};

void foo()
{
//    A *p = new A;//需要手动释放
//    delete p;

    auto_ptr<A> p(new A); //智能指针,离开作用域自动释放内存
    p->func();
    (*p).func(); //等价于上一行
}

int main()
{
    foo();
    return 0;
}

实现auto_ptr类似功能

主要利用重载 * 和 -> 运算符,实现对资源的访问。

类名& operator*() (?? operator. ??)
类名* operator->()


#include <iostream>
#include <memory>
using namespace std;
class A
{
public:
    A(){
        cout<<"A()"<<endl;
    }
    ~A(){
        cout<<"~A()"<<endl;
    }
    void func(){
        cout<<"hahaha"<<endl;
    }
};

class SmartPtr{
public:
    SmartPtr(A * pa){
        _pa= pa;
    }
    ~SmartPtr(){
        delete _pa;
    }
    A& operator*(){
        return *_pa;
    }
    A* operator->(){
        return _pa;
    }


    A& get_pa(){
       return *_pa;
    }
    A* get_pa_ptr(){
        return _pa;
    }



private:
    A * _pa;
};

void foo()
{
    SmartPtr sp(new A);


    sp.get_pa().func();
    sp->func();//等价于上一行 重载了*
    sp.get_pa_ptr()->func();
    sp->func(); //等价于上一行 重载了->

}

int main()
{
    foo();
    return 0;
}
  • 19
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可能只会写BUG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值