boost::scoped_ptr特点:
boost::scoped_ptr的实现和std::auto_ptr非常类似,都是利用了一个栈上的对象去管理一个堆上的对象,从而使得堆上的对象随着栈上的对象销毁时自动删除。不同的是,boost::scoped_ptr有着更严格的使用限制——不能拷贝。这就意味着:boost::scoped_ptr指针是不能转换其所有权的。
-
不能转换所有权
boost::scoped_ptr所管理的对象生命周期仅仅局限于一个区间(该指针所在的"{}"之间),无法传到区间之外,这就意味着boost::scoped_ptr对象是不能作为函数的返回值的(std::auto_ptr可以)。 -
不能共享所有权
这点和std::auto_ptr类似。这个特点一方面使得该指针简单易用。另一方面也造成了功能的薄弱——不能用于stl的容器中。 -
不能用于管理数组对象
由于boost::scoped_ptr是通过delete来删除所管理对象的,而数组对象必须通过deletep[]来删除,因此boost::scoped_ptr是不能管理数组对象的,如果要管理数组对象需要使用boost::scoped_array类。
它的常用操作如下:
成员函数 |
功能 |
operator*() |
以引用的形式访问所管理的对象的成员 |
operator->() |
以指针的形式访问所管理的对象的成员 |
reset() |
释放所管理的对象,管理另外一个对象 |
swap(scoped_ptr& b) |
交换两个boost::scoped_ptr管理的对象 |
class TestSample
{
public:
TestSample()
{
cout << "TestSample Constructor...!" << endl;
};
~TestSample()
{
cout << "TestSample Destructor...!" << endl;
};
void PrintSomething()
{
cout << "TestSample PrintSomething..." << endl;
};
private:
int num = 0;
};
int _tmain(int argc, _TCHAR* argv[])
{
boost::scoped_ptr<TestSample> sp1(new TestSample());
sp1->PrintSomething();
sp1.reset();
return 0;
}
输出:
TestSample Constructor...!
TestSample PrintSomething...
TestSample Destructor...!
请按任意键继续. . .
scoped_array说明:
接受数组指针new []