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的一些库,比如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 用法总结
- [zz]boost/shared_ptr 用法总结
- boost shared_ptr用法(转)
- shared_ptr的用法
- Caffe错误boost::python::register_ptr_to_python<boost::shared_ptr<Blob<Dtype> > >();
- boost之shared_ptr
- boost之shared_ptr
- boost 智能指针 shared_ptr 原理分析
- std::unique_ptr和std::shared_ptr的用法
- C++ shared_ptr的用法
- boost之shared_ptr的一个计数小问题
- boost::shared_ptr在类和容器中使用
- [C++] Boost智能指针——boost::shared_ptr(使用及原理分析)
- boost::scoped_ptr用法
- [6]智能指针boost::shared_ptr
- 【C++/Boost Lib】如何初始化一个shared_ptr指针。
- boost::shared_ptr 智能指针在项目中的使用
- auto_ptr、shared_ptr、weak_ptr、scoped_ptr用法小结
- boost::weak_ptr和enable_shared_from_this
- boost>shared_ptr