最近在学习智能指针这块,提到智能指针就不得不提到Boost库里的智能指针,在Boost库里实现了五大智能指针的类模板
1、template<typename T> class scoped_ptr;
2、template<typename T> class scoped_array;
3、template<typename T> class shared_ptr;
4、template<typename T> class shared_array;
5、template<typename T> class weak_ptr;
1和2都是防止拷贝和赋值的
3和4都是采用引用的引用计数版的简化版智能指针SharedPtr
5是采用弱引用计数智能指针
下面给出详细的配置Boost的步骤
1、首先在在Boost库官网上下载版本为Boost_1_59_0 版本的
2、下载完成之后将压缩包解压到一个目录下:
3、然后打开这个文件夹发现里面有一个boostrap.bat
4、双击运行这个.bat文件
会发现多了一个boostcpp.bat
、
5、此时双击运行这个boostcpp.bat
6、此时再运行这个exe程序运行结束会自动关闭boost的安装就完成了
7、接下来再VS2013里对它进行配置
1>首先打开VS新建一个控制台应用程序
2、然后在里面添加一个main.cpp的源文件
添加如下代码:
#include "stdafx.h"
#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace std;
int main()
{
using boost::lexical_cast;
int a = lexical_cast<int>("123");
double b = lexical_cast<double>("123.0123456789");
string s0 = lexical_cast<string>(a);
string s1 = lexical_cast<string>(b);
cout << "number: " << a << " " << b << endl;
cout << "string: " << s0 << " " << s1 << endl;
int c = 0;
try{
c = lexical_cast<int>("abcd");
}
catch (boost::bad_lexical_cast& e){
cout << e.what() << endl;
}
system("pause");
return 0;
}
3、然后在这个项目的属性里修改如下信息:
4、此时再运行这个程序,会打印出如下信息则说明VS环境下配置Boost库已经成功了
当时在开始下载安装包的时候下载的是Boost_1_64_0,然后在运行exe程序的时候一直出现内部编译错误。可能是这个版本不支持VS2013。