boost库之shared_ptr库,智能指针

使用说明:

        1. shared_ptr<int>默认值是NULL

        2. 赋值

        3.reset()

        4. 函数返回share_ptr<int>为NULL


代码:

#include <iostream>
#include <string>

#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost;

//4. return shared_ptr<int> is null
shared_ptr<int> func()
{
        return shared_ptr<int>();  }

int main(int argc, char* argv[])
{
        //1. shared_ptr<int> default is null
        shared_ptr<int> ptr;

        //1.1
        if (ptr == NULL)
                cout<<"1. \"shared_ptr<int> ptr\" is null"<<endl;
        else
                cout<<"1. \"shared_ptr<int> ptr\" is not null"<<endl;


        //2. assgin value to share_ptr<int>
        ptr = shared_ptr<int>(new int(100));

        //2.1
        cout<<"2. ptr:"<<ptr<<", *ptr:"<<*ptr<<endl;


        //3. shared_ptr<int> reset() is null
        ptr.reset();

        //3.1
        if (ptr == NULL)
                cout<<"3. \"ptr.reset()\" is null"<<endl;
        else
                cout<<"3. \"ptr.reset()\" is not null"<<endl;


        //4.1
        if (func() == NULL)
                cout<<"4. \"func() return share_ptr<int>()\" is null"<<endl;
        else
                cout<<"4. \"func() return share_ptr<int>()\" is not null"<<endl;


        return 0;
}

输出结果:



参考资料:

        boost::shared_ptr class example:http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/shared_ptr.htm#example

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值