目录
1.shared_ptr
使用例子
2.weak_ptr
使用例子
3.scoped_ptr
使用例子
4.shared_array与scoped_array
使用例子
1.shared_ptr ^
shared_ptr智能指针也跟其名字一样,共享的,允许多个该智能指针共享地“拥有”同一堆分配对象的内存。实现上采用了引用计数,只有在引用计数归零时,才会真正释放所占有的堆内存的空间。
使用例子 ^
#include <iostream>
#include <boost/smart_ptr/shared_ptr.hpp>
using namespace boost;
using std::cout;
using std::endl;
struct A
{
int a = 10;
};
int main()
{
A *p = new A;
shared_ptr<A> ptr(new A(*p));
shared_ptr<A> ptr1(ptr);
cout << "判断目标是否是原始数据:"<<ptr1.owner_before(shared_ptr<A>(new A(*p))) << endl;
cout << "共享指针的数量:" << ptr.use_count() << endl;
cout << "通过指针访问成员:" << ptr.get()->a << endl;
cout << "是否是单个:"