国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Boost智能指針——scoped_ptr

boost::scoped_ptrstd::auto_ptr非常類似,是一個(gè)簡單的智能指針,它能夠保證在離開作用域后對象被自動(dòng)釋放。下列代碼演示了該指針的基本應(yīng)用:

#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";
}

 

該代碼的輸出結(jié)果是:

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

可以看到:當(dāng)implementation類離其開impl作用域的時(shí)候,會被自動(dòng)刪除,這樣就會避免由于忘記手動(dòng)調(diào)用delete而造成內(nèi)存泄漏了。

boost::scoped_ptr特點(diǎn):

boost::scoped_ptr的實(shí)現(xiàn)和std::auto_ptr非常類似,都是利用了一個(gè)棧上的對象去管理一個(gè)堆上的對象,從而使得堆上的對象隨著棧上的對象銷毀時(shí)自動(dòng)刪除。不同的是,boost::scoped_ptr有著更嚴(yán)格的使用限制——不能拷貝。這就意味著:boost::scoped_ptr指針是不能轉(zhuǎn)換其所有權(quán)的。

  1. 不能轉(zhuǎn)換所有權(quán)
    boost::scoped_ptr所管理的對象生命周期僅僅局限于一個(gè)區(qū)間(該指針?biāo)诘?{}"之間),無法傳到區(qū)間之外,這就意味著boost::scoped_ptr對象是不能作為函數(shù)的返回值的(std::auto_ptr可以)。
  2. 不能共享所有權(quán)
    這點(diǎn)和std::auto_ptr類似。這個(gè)特點(diǎn)一方面使得該指針簡單易用。另一方面也造成了功能的薄弱——不能用于stl的容器中。
  3. 不能用于管理數(shù)組對象
    由于boost::scoped_ptr是通過delete來刪除所管理對象的,而數(shù)組對象必須通過deletep[]來刪除,因此boost::scoped_ptr是不能管理數(shù)組對象的,如果要管理數(shù)組對象需要使用boost::scoped_array類。

boost::scoped_ptr的常用操作:

可以簡化為如下形式:

namespace boost {

    template<typename T> class scoped_ptr : noncopyable {
    public:
        explicit scoped_ptr(T* p = 0);
        ~scoped_ptr();

        void reset(T* p = 0);

        T& operator*() const;
        T* operator->() const;
        T* get() const;

        void swap(scoped_ptr& b);
    };

    template<typename T>
    void swap(scoped_ptr<T> & a, scoped_ptr<T> & b);
}

 

它的常用操作如下:

成員函數(shù)

功能

operator*()

以引用的形式訪問所管理的對象的成員

operator->()

以指針的形式訪問所管理的對象的成員

get()

釋放所管理的對象,管理另外一個(gè)對象

swap(scoped_ptr& b)

交換兩個(gè)boost::scoped_ptr管理的對象

 

下列測試代碼演示了這些功能函數(shù)的基本使用方法。

#include <string>
#include <iostream>

#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>

#include <boost/config.hpp>
#include <boost/detail/lightweight_test.hpp>

void test()
{
    // test scoped_ptr with a built-in type
    long * lp = new long;
    boost::scoped_ptr<long> sp ( lp );
    BOOST_TEST( sp.get() == lp );
    BOOST_TEST( lp == sp.get() );
    BOOST_TEST( &*sp == lp );

    *sp = 1234568901L;
    BOOST_TEST( *sp == 1234568901L );
    BOOST_TEST( *lp == 1234568901L );

    long * lp2 = new long;
    boost::scoped_ptr<long> sp2 ( lp2 );

    sp.swap(sp2);
    BOOST_TEST( sp.get() == lp2 );
    BOOST_TEST( sp2.get() == lp );

    sp.reset(NULL);
    BOOST_TEST( sp.get() == NULL );

}

void main()
{
    test();
}

 

boost::scoped_ptr和std::auto_ptr的選?。?

boost::scoped_ptr和std::auto_ptr的功能和操作都非常類似,如何在他們之間選取取決于是否需要轉(zhuǎn)移所管理的對象的所有權(quán)(如是否需要作為函數(shù)的返回值)。如果沒有這個(gè)需要的話,大可以使用boost::scoped_ptr,讓編譯器來進(jìn)行更嚴(yán)格的檢查,來發(fā)現(xiàn)一些不正確的賦值操作。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
【轉(zhuǎn)】C++ 智能指針詳解
C 智能指針詳解
智能指針
C++11中的智能指針
boost::shared
Boost智能指針
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服