`

智能指针 - scoped_ptr

阅读更多

由于最近研究的一个项目是用C++开发的, 所以不得不重温一下C++, 自己刚开始工作时候用的就是C++, 好熟悉的感觉.

智能指针——scoped_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.                             

 

From:
Boost智能指针——scoped_ptr

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics