可能是重复问题:
什么是智能指针,何时使用?
我正在阅读一篇文章,其中有一个小例子演示了如何使用boost::scoped_ptr<T>
:
#include <cstdlib>
#include <iostream>
#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
static int count = 0;
class printer
{
int m_id;
public:
printer(void) :
m_id(count++)
{
}
~printer(void)
{
std::cout << "Printer " << m_id
<< " destroyed" << std::endl;
}
};
int
main(void)
{
boost::scoped_ptr<printer> p1(new printer);
boost::scoped_ptr<printer> p2(new printer);
std::cout << "Exiting test program" << std::endl;
return EXIT_SUCCESS;
}
文章中唯一让我不明白的是这句话:使用
scoped_ptr
表明不打算或不允许所有权转移。
也许对于初学者来说,这篇文章并不适合。但是上面这句话到底是什么意思呢?