您的位置:首页 > 其它

boost shared_ptr 用法

2017-07-10 10:55 309 查看
1 shared_ptr 不明确的符号
boost的一些库,比如share_ptr已经被高版本的stl采纳了.如果在代码中没有指定命名空间,编译器就无法得知应该使用哪个命名空间之下的代码进行编译如下引入的命名空间的方式不推荐
using namespace std;
using namespace tr1;
using namespace boost;
实际上两个智能指针是一样的,你只需要用一个即可.如果你一定要两个都用,那么不要写上面的语句.而是应该写std::tr1::xxxx, boost:xxxx

2 shared_ptr 作为类成员函数的写法
#include <iostream>
#include <boost/shared_ptr.hpp>

struct SStudentInfo
{
public:
SStudentInfo()
{
std::cout << "Create instance" << std::endl;
};

~SStudentInfo()
{
std::cout << "Free instance" << std::endl;
}
};

class CTestSharedPtr
{
public:

CTestSharedPtr()
{
//m_student = new SStudentInfo();//error
m_student = boost::shared_ptr<SStudentInfo>(new SStudentInfo());
};

~CTestSharedPtr() {};
private:

boost::shared_ptr<SStudentInfo> m_student;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  boost shared_ptr