Boost.smart_ptr.scoped_ptr--1
2013-04-28 09:24
288 查看
smart_ptr中的scoped_ptr智能指针可以避免C++中new了对象,没有delete造成的对象不能正确析构的现象,但是scoped_ptr
只能用在局部,它内部保存的指针不能传递,不能改变,不能赋值给其他指针,也就是具有唯一性的读;下面是测试程序;
// scoped_ptr.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <boost/smart_ptr.hpp>
#include <string>
using namespace boost;
using namespace std;
/*=================================================================
*scoped_ptr说明:
(1)scoped_ptr指针类只能用在一个作用域内(如仅限于一个函数),不能被拷贝,再赋值;
(2)scoped_ptr的用法:在原本接收new表达式出来的地方改用scoped_ptr对象;
如 scoped_ptr<T> sp(new T());
scoped_ptr<T> sp(new T());
(3)scoped_ptr只能使用*和->操作sp不能使用++ --,记住此sp不能被拷贝和赋值给其他指针哦;
可以将scoped_ptr理解为定义了一个指针常量;
(4)使用scoped_ptr这个指针,不用再通过delete删除堆上对象了,它会自动的调用析构;
=================================================================*/
class Test
{
public:
Test()
{
cout<<"Test object is create"<<endl;
}
~Test()
{
cout<<"Test object is automate delete"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
scoped_ptr<int> spi(new int(4));
cout<<"new出int型指针:"<<*spi<<endl;
scoped_ptr<string> sps(new string("text"));
cout<<"new出的string指针"<<*sps<<"长度:"<<sps->size()<<endl;
Test *tt=new Test();//不会自动析构的
scoped_ptr<Test> spTest(new Test());
return 0;
}
执行结果:可以发现不用delete,new出的对象不会析构,但是使用智能指针scoped_ptr会自动析构;
只能用在局部,它内部保存的指针不能传递,不能改变,不能赋值给其他指针,也就是具有唯一性的读;下面是测试程序;
// scoped_ptr.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <boost/smart_ptr.hpp>
#include <string>
using namespace boost;
using namespace std;
/*=================================================================
*scoped_ptr说明:
(1)scoped_ptr指针类只能用在一个作用域内(如仅限于一个函数),不能被拷贝,再赋值;
(2)scoped_ptr的用法:在原本接收new表达式出来的地方改用scoped_ptr对象;
如 scoped_ptr<T> sp(new T());
scoped_ptr<T> sp(new T());
(3)scoped_ptr只能使用*和->操作sp不能使用++ --,记住此sp不能被拷贝和赋值给其他指针哦;
可以将scoped_ptr理解为定义了一个指针常量;
(4)使用scoped_ptr这个指针,不用再通过delete删除堆上对象了,它会自动的调用析构;
=================================================================*/
class Test
{
public:
Test()
{
cout<<"Test object is create"<<endl;
}
~Test()
{
cout<<"Test object is automate delete"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
scoped_ptr<int> spi(new int(4));
cout<<"new出int型指针:"<<*spi<<endl;
scoped_ptr<string> sps(new string("text"));
cout<<"new出的string指针"<<*sps<<"长度:"<<sps->size()<<endl;
Test *tt=new Test();//不会自动析构的
scoped_ptr<Test> spTest(new Test());
return 0;
}
执行结果:可以发现不用delete,new出的对象不会析构,但是使用智能指针scoped_ptr会自动析构;
相关文章推荐
- boost.smart_ptr-智能指针scoped_ptr
- boost smart_ptr -> scoped_ptr
- boost.smart_ptr-智能指针scoped_array
- boost.smart_ptr-智能指针scoped_array
- Boost源码剖析--<boost/smart_ptr/scoped_ptr.hpp>
- boost.smart_ptr-智能指针scoped_ptr
- boost.smart_ptr-智能指针scoped_ptr
- boost.smart_ptr-智能指针scoped_ptr
- boost___smart_ptr
- boost::scoped_ptr
- boost.smart_ptr库简介
- boost之scoped_ptr
- c++智能指针:boost::scoped_ptr,boost::shared_ptr,boost::scoped_array
- boost::scoped_ptr()
- Boost.smart_ptr.shared_ptr--2
- boost源码剖析1----内存管理scoped_ptr
- [C++] Boost智能指针——boost::scoped_ptr(使用及原理分析)
- smart_ptr学习之scoped_ptr
- 初探boost之smart_ptr库学习笔记
- Boost 学习(一) scoped_ptr