boost smart pointer 笔记

本文主要介绍了Boost库中的两种智能指针:scoped_ptr和shared_ptr的使用。scoped_ptr具有所有权的概念,不可拷贝,对象在析构时自动删除。shared_ptr支持引用计数,可以被多个对象共享,自定义删除器,并可用于接口编程。文章通过实例展示了如何使用这两种智能指针进行内存管理。
摘要由CSDN通过智能技术生成
/*
 * =====================================================================================
 *
 *       Filename:  T_smartpointer.cpp
 *
 *    Description:   smartpointer of boost
 *
 *        Version:  1.0
 *        Created:  2008年12月11日 13时25分54秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li WeiJian (mn), lwj1396@163.com
 *        Company:  hunan university
 *
 * =====================================================================================
 */

#include<boost/scoped_ptr.hpp>
#include<boost/shared_ptr.hpp>
#include<boost/utility.hpp>
#include<iostream>


//============================= scoped_ptr==============================================
//
struct Shoe{~Shoe(){std::cout<<"Buckle my shoe!"<<std::endl;}};

class Myclass
{
    boost::scoped_ptr<int> ptr;
public:
    Myclass():ptr(new int){*ptr = 0;}
    ~Myclass(){std::cout<<"destructor of Myclass/n";}
    int add_one() {return ++*ptr;}
};

class example : private boost::noncopyable
{
private:
    boost::scoped_ptr<Myclass>  _imp;//hide the implement detail
public:
    example():_imp(new Myclass){}
    ~example(){std::cout<<"destructor of example/n";}
    int dothing(){return _imp->add_one();}
};

void Test_scoped_ptr()
{
    boost::scoped_ptr<Shoe> x(new Shoe);
    x.reset();//reset()的时候就删除了该对象
    example e;
    example b;
    /* 出错,因为是noncopyable
    example *p=&e;
    *p=b;
    or e=b
    */
    std::cout<<e.dothing()<<"/n";
}
//end scoped_ptr



//===================================shared_ptr==============================
//
// interface programming

//x.h
class X
{
public:
    virtual void f()=0;
    virtual void g()=0;
protected:
    ~X(){}
};
//share_ptr<X> createX();

//x.cpp
class X_impl:public X
{
private:
    X_impl(X_impl const &);
    X_impl & operator=(X_impl const &);
    ~X_impl(){};//不能被delete,且不能声明static的X_impl全局对象了,即不能创建在栈上

public:
    class deleter;
    friend class deleter;
    class deleter
    {
    public:
        void operator()(X_impl* p)
        {
            std::cout<<"X_imple deleted by deleter/n";
            delete p;
        }
    };

    X_impl(){}
    virtual void f()
    {
        std::cout<<"X_impl f()/n";
    }
    virtual void g()
    {
        std::cout<<"X_impl g()/n";
    }
};

boost::shared_ptr<X> createX()
{
    //自定义了删除器
    boost::shared_ptr<X> px(new X_impl,X_impl::deleter());
    return px;
}



//====包装 static的东西,引用计数为0时候,依然没有删除
class A
{
    public:
    void    f(){std::cout<<"i am a A/n";}
};
struct null_deleter
{
    void operator()(void const *) const
    {}
};

static A x;

boost::shared_ptr<A> createX_impl()
{
    boost::shared_ptr<A> px(&x,null_deleter());
    return px;
}
//==================




//===================从一个raw pointer处获得share_ptr
void f(boost::shared_ptr<A> pa)   //会隐式转换?
{}

//对于成员函数而言,可以在第一个参数处转换this
//void f(boost::shared_ptr<A> this_, int m)

//===================




//====================shared from this=====================
class impl:public boost::enable_shared_from_this<impl>
{
    public:
        virtual shared_ptr<impl> getImpl()
        {
            return boost::shared_from_this();
        }
        
};

//====================



//===============shared_ptr<void> 相当于void*========

boost::shared<void> pa(new A);

//===============




void Test_share_ptr()
{
//    boost::shared_ptr<X> p=createX();
    //p.reset(); 可以reset,但是X_impl会被销毁,后边就不能再调用函数
//    p->f();
    //delete p.get(); 因为~X()是protected,所以此处会有错
    boost::shared_ptr<A> i=createX_impl();
    i->f();
}


int main()
{
    //Test_scoped_ptr();
    Test_share_ptr();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值