您的位置:首页 > 运维架构

智能指针

2015-04-21 15:03 423 查看
本文转载地址:http://blog.csdn.net/alex_my/article/details/17636561

测试环境:win7, vs2012

 如果未安装boost,请参考:http://blog.csdn.net/alex_my/article/details/17630685

 涉及智能指针:shared_ptr, weak_ptr, scoped_ptr, auto_ptr

 其它:enable_shared_from_this

 总调用函数: testSmartPointer()

 可以将其放在main()中运行。解释在代码中。

[cpp] view
plaincopyprint?





<span style="font-size:18px;">#include <string>  

#include <vector>  

#include <iostream>  

#include <boost/scoped_ptr.hpp>  

#include <boost/shared_ptr.hpp>  

#include <boost/weak_ptr.hpp>  

#include <boost/enable_shared_from_this.hpp>  

  

class Base  

{  

public:  

    explicit Base(int a)  

    : m_a(a)  

    {  

    }  

    virtual ~Base()  

    {  

    }  

  

    int GetA() const  

    {  

        return m_a;   

    }  

  

private:  

    int m_a;  

};  

  

class Derive : public Base  

{  

public:  

    explicit Derive(int b)  

        : Base(2 * b)  

        , m_b(b)  

    {  

  

    }  

  

    virtual ~Derive()  

    {  

    }  

  

    int GetB() const  

    {  

        return m_b;   

    }  

  

private:  

    int m_b;  

};  

  

class EnableShared  

{  

public:  

    EnableShared()  

    : m_e(3)  

    {  

  

    }  

    ~EnableShared()   

    {  

        std::cout<< "EnableShared Destruction execute" << std::endl;  

    }  

  

    void ShowE()  

    {  

        boost::shared_ptr<EnableShared> p1(this);  

        std::cout<< p1->m_e << std::endl;  

    }  

  

private:  

    int m_e;  

};  

  

class EnableSharedEx : public boost::enable_shared_from_this<EnableSharedEx>  

{  

public:  

    EnableSharedEx()  

        : m_e(3)  

    {  

  

    }  

    ~EnableSharedEx()   

    {  

        std::cout<< "EnableSharedEx Destruction execute" << std::endl;  

    }  

  

    void ShowE()  

    {  

        //boost::shared_ptr<EnableSharedEx> p1(this);  

        boost::shared_ptr<EnableSharedEx> p1 = shared_from_this();  

        std::cout<< p1->m_e << std::endl;  

    }  

  

private:  

    int m_e;  

};  

  

static void testSharedPtr();  

static void testEnableSharedFromthis();  

static void testScopedPtr();  

static void testAutoPtr();  

  

void testSmartPointer()  

{  

    // ------------- shared_ptr -------------  

    testSharedPtr();  

  

    // ------------- enable_shared_from_this -------------  

    testEnableSharedFromthis();  

  

    // ------------- scoped_ptr -------------  

    testScopedPtr();  

  

    // ------------- auto_ptr -------------  

    testAutoPtr();  

  

    // ------------- summary -------------  

    // 1 auto_ptr会转移所有权,使原拥有者失效  

    // 2 shared_ptr比起auto_ptr,不会转移所有权,而是增加引用计数  

    // 3 scoped_ptr不允许复制  

    // 4 weak_ptr起了类似于观察者的作用,不会对拥有者造成影响  

}  

  

void testSharedPtr()  

{  

    // 1 使用  

    boost::shared_ptr<Base> pa(new Base(2));  

    std::cout<< "testSharedPtr" << pa->GetA() << std::endl;  

  

    // 2 发生引用,此时pa2和pa指向同一个指针,观察计数器share_ptr::use_count_ 值从1变为2。  

    boost::shared_ptr<Base> pa2 = pa;  

  

    // 3 弱引用,计数器并仍然是2,不过weak_count_ 从1变成了2。  

    boost::weak_ptr<Base> p3 = pa;  

}  

  

void testEnableSharedFromthis()  

{  

    // 1 应用举例  

    boost::shared_ptr<EnableShared> pe(new EnableShared);  

    //pe->ShowE();  

  

    // 2 注释说明  

    // 编译可以通过,但是析构函数会执行两次,造成程序崩溃  

    // shared_ptr的一个缺点,无法从this指针构造,无法像testSharedPtr中的引用例子一样。  

  

    // 3 解决办法 enable_shared_from_this,改写EnableShared为EnableSharedEx  

    boost::shared_ptr<EnableSharedEx> pex(new EnableSharedEx);  

    pex->ShowE();  

}  

  

void testScopedPtr()  

{  

    // 1 应用举例、  

    boost::scoped_ptr<Base> pb(new Base(2));  

    std::cout << "testScopedPtr" << pb->GetA() << std::endl;  

  

    // 2 引用,无法通过编译,原因:scope_ptr不允许复制  

    // boost::scoped_ptr<Base> pb2 = pb;  

}  

  

void testAutoPtr()  

{  

    // 1 应用举例,与shared_ptr相似  

    std::auto_ptr<Base> pa(new Base(2));  

    std::cout<< "testAutoPtr: " << pa->GetA() << std::endl;  

  

    // 2 发生引用,与shared_ptr不同的地方在于pa编程空指针了。  

    std::auto_ptr<Base> pax = pa;  

}</span>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息