boost::smart_ptr之智能指针

来源:互联网 发布:淘宝做什么产品赚钱 编辑:程序博客网 时间:2024/06/05 05:28

前言:


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;}
运行结果:


0 0