现在的位置: 首页 > 综合 > 正文

boost库之scoped_ptr

2013年10月10日 ⁄ 综合 ⁄ 共 558字 ⁄ 字号 评论关闭
scoped:范围的意思
// scoped_ptr_Exam.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

#include "boost/smart_ptr.hpp"
using namespace boost;

int _tmain(int argc, _TCHAR* argv[])
{

	scoped_ptr<string> sp(new string("hello world"));
	std::cout << *sp << std::endl;
	std::cout << sp->size() << std::endl;
	
	sp.reset();
	/*
	sp++;							// scoped_ptr未定义递增操作符
	scoped_ptr<string> sp2 = sp;	// scoped_ptr不能拷贝赋值
	*/
	scoped_ptr和auto_ptr有同样的缺陷-不能用作容器的元素,但原因不同:
	auto_ptr是因为它的转移语义,而scoped_ptr则是因为不支持拷贝赋值,不符合
	容器对元素类型的要求(C++标准如是说:“STL元素必须具备拷贝构造和可赋值……”)
	
	return 0;
}

抱歉!评论已关闭.