Boost智能指针——scoped_ptr

来源:互联网 发布:微信公众网页授权域名 编辑:程序博客网 时间:2024/05/22 00:29

boost智能指针——scoped_ptr

boost::scoped_ptrstd::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放。下列代码演示了该指针的基本应用


#include <string>
#include <iostream>
#include <boost/scoped_ptr.hpp>

class implementation
{
public:
    ~implementation() { std::cout <<"destroying implementation\n"; }
    void do_something() { std::cout << "did something\n"; }
};

void test()
{
    boost::scoped_ptr<implementation> impl(new implementation());
    impl->do_something();
}

void main()
{
    std::cout<<"Test Begin ... \n";
    test();
    std::cout<<"Test End.\n";
}

 

该代码的输出结果是:

Test Begin ...
did something
destroying implementation
Test End.

可以看到:当implementation类离其开impl作用域的时候,会被自动删除,这样就会避免由于忘记手动调用delete而造成内存泄漏了。

boost::scoped_ptr特点:

boost::scoped_ptr的实现和std::auto_ptr非常类似,都是利用了一个栈上的对象去管理一个堆上的对象,从而使得堆上的对象随着栈上的对象销毁时自动删除。不同的是,boost::scoped_ptr有着更严格的使用限制——不能拷贝。这就意味着:boost::scoped_ptr指针是不能转换其所有权的。

  1. 不能转换所有权
    boost::scoped_ptr所管理的对象生命周期仅仅局限于一个区间(该指针所在的"{}"之间),无法传到区间之外,这就意味着boost::scoped_ptr对象是不能作为函数的返回值的(std::auto_ptr可以)。
  2. 不能共享所有权
    这点和std::auto_ptr类似。这个特点一方面使得该指针简单易用。另一方面也造成了功能的薄弱——不能用于stl的容器中。
  3. 不能用于管理数组对象
    由于boost::scoped_ptr是通过delete来删除所管理对象的,而数组对象必须通过deletep[]来删除,因此boost::scoped_ptr是不能管理数组对象的,如果要管理数组对象需要使用boost::scoped_array类。

    scoped_ptr的常用操作:
    // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
    // automatically deletes the pointer it holds (if any).
    // That is, scoped_ptr<T> owns the T object that it points to.
    // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
    // Also like T*, scoped_ptr<T> is thread-compatible, and once you
    // dereference it, you get the threadsafety guarantees of T.
    //
    // The size of a scoped_ptr is small:
    // sizeof(scoped_ptr<C>) == sizeof(C*)
    template<class C>
    class scoped_ptr {
    public:


    // The element type
    typedef C element_type;


    // Constructor.  Defaults to initializing with NULL.
    // There is no way to create an uninitialized scoped_ptr.
    // The input parameter must be allocated with new.
    explicit scoped_ptr(C* p = NULL) :
    ptr_(p) {
    }


    // Destructor.  If there is a C object, delete it.
    // We don't need to test ptr_ == NULL because C++ does that for us.
    ~scoped_ptr() {
    enum {
    type_must_be_complete = sizeof(C)
    };
    delete ptr_;
    }


    // Reset.  Deletes the current owned object, if any.
    // Then takes ownership of a new object, if given.
    // this->reset(this->get()) works.
    void reset(C* p = NULL) {
    if (p != ptr_) {
    enum {
    type_must_be_complete = sizeof(C)
    };
    delete ptr_;
    ptr_ = p;
    }
    }


    // Accessors to get the owned object.
    // operator* and operator-> will assert() if there is no current object.
    C& operator*() const {
    assert(ptr_ != NULL);
    return *ptr_;
    }
    C* operator->() const {
    assert(ptr_ != NULL);
    return ptr_;
    }
    C* get() const {
    return ptr_;
    }


    // Comparison operators.
    // These return whether two scoped_ptr refer to the same object, not just to
    // two different but equal objects.
    bool operator==(C* p) const {
    return ptr_ == p;
    }
    bool operator!=(C* p) const {
    return ptr_ != p;
    }


    // Swap two scoped pointers.
    void swap(scoped_ptr& p2) {
    C* tmp = ptr_;
    ptr_ = p2.ptr_;
    p2.ptr_ = tmp;
    }


    // Release a pointer.
    // The return value is the current pointer held by this object.
    // If this object holds a NULL pointer, the return value is NULL.
    // After this operation, this object will hold a NULL pointer,
    // and will not own the object any more.
    C* release() WARN_UNUSED_RESULT {
    C* retVal = ptr_;
    ptr_ = NULL;
    return retVal;
    }


    private:
    C* ptr_;


    // Forbid comparison of scoped_ptr types.  If C2 != C, it totally doesn't
    // make sense, and if C2 == C, it still doesn't make sense because you should
    // never have the same object owned by two different scoped_ptrs.
    template<class C2> bool operator==(scoped_ptr<C2> const& p2) const;
    template<class C2> bool operator!=(scoped_ptr<C2> const& p2) const;


    // Disallow evil constructors
    scoped_ptr(const scoped_ptr&);
    void operator=(const scoped_ptr&);
    }


    它的常用操作如下:

    成员函数

    功能

    operator*()

    以引用的形式访问所管理的对象的成员

    operator->()

    以指针的形式访问所管理的对象的成员

    get()

    释放所管理的对象,管理另外一个对象

    swap(scoped_ptr& b)

    交换两个boost::scoped_ptr管理的对象




原创粉丝点击