首页 > 其他 > 详细

shared_ptr

时间:2014-03-03 16:53:48      阅读:282      评论:0      收藏:0      [点我收藏+]

如果要使用boost的shared_ptr要知道与VS自带的那个shared_ptr避免冲突。

因为在VS的include目录下有一个全局的shared_ptr,所以应该像如下这样正规的使用boost的shared_ptr


boost的所有智能指针都在smart_ptr.hpp头文件中。


#include <vector>
#include <boost/smart_ptr.hpp>
#include <iostream>
using namespace std;
class test;
typedef boost::shared_ptr<test> test_ptr ;

class test
{
public:
	test(){cout<<"a test object created"<<endl;}
	~test(){cout<<"a test object destroyed"<<endl;}
	void print(void) const {cout<<"test print"<<endl;}
};

int main()
{ 
	test_ptr pt(new test);
	vector<test_ptr> test_ptr_array;
	for (int i=0;i<10;i++)
	{
		test_ptr_array.push_back(pt);
	}
	for (int i=0;i<10;i++)
	{
		test_ptr_array[i]->print();
	}
	return 0;
}

a test object created
test print
test print
test print
test print
test print
test print
test print
test print
test print
test print
a test object destroyed
请按任意键继续. . .

shared_ptr,布布扣,bubuko.com

shared_ptr

原文:http://blog.csdn.net/calmreason/article/details/20306073

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!