boost库之shared_ptr库,智能指针

来源:互联网 发布:氧气听书软件电脑版 编辑:程序博客网 时间:2024/06/05 01:58

使用说明:

        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 nullshared_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

0 0