首页 > 其他 > 详细

boost::smart_ptr之智能指针

时间:2015-03-27 17:26:55      阅读:263      评论:0      收藏:0      [点我收藏+]

前言:


1.scoped_ptr 智能指针使用说明

示例代码:

#include "stdafx.h"
#include <boost/smart_ptr.hpp>
#include <iostream>

using namespace std;
using namespace boost;

class posix_file
{
public:
	posix_file(const char *  file_name)
	{
		cout << "open file!   " << *file_name << endl;
 	}
	~posix_file()
	{
		cout << "close file!" << endl;
	}
};

int main(int argc, _TCHAR* argv[])
{
	/************************************************************************/
	/*scoped_ptr不允许赋值,拷贝,不支持--和++,只能在被声明的作用域内使用  */
	/************************************************************************/
	scoped_ptr<string> sptr(new string("test"));
	cout <<  "指针内容为:" << (*sptr).c_str()  << endl;
	cout <<  "指针内容长度为:" << sptr->size()  << endl;
	scoped_ptr<int> iptr (new int);
	*iptr = 1000;
	cout << *iptr << endl;
	{
		scoped_ptr<posix_file> fptr(new posix_file("/file/filename"));
	}

	while (1);
	return 0;
}
运行结果:

技术分享

boost::smart_ptr之智能指针

原文:http://blog.csdn.net/qingzai_/article/details/44678417

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