boost智能指针

scoped_ptr

代码:

#include <iostream>
#include <boost/scoped_ptr.hpp>

using namespace std;

class X
{
public:
	X() { cout << "X..." << endl; }
	~X() { cout << "~X..." << endl; }
};

int main()
{
	cout << "Entering main..." << endl;
	{
		boost::scoped_ptr<X> p(new X);

		//boost::scoped_ptr<X> p2(p);  //不能拷贝和赋值
	}

	cout << "Exiting main..." << endl;
	return 0;

}

运行结果:
在这里插入图片描述

shared_ptr

代码:

#include <iostream>
#include <boost/shared_ptr.hpp>

using namespace std;

class X
{
public:
	X() { cout << "X..." << endl; }
	~X() { cout << "~X..." << endl; }
};

int main()
{
	cout << "Entering main..." << endl;

	boost::shared_ptr<X> p1(new X);
	cout << p1.use_count() << endl;

	boost::shared_ptr<X> p2 = p1;
	cout << p1.use_count() << endl;

	p1.reset();
	cout << p2.use_count() << endl;

	p2.reset();

	cout << "Exiting main..." << endl;
	return 0;
}

运行结果:
在这里插入图片描述
跟踪调试内部实现看不懂。涉及到原子性自增,线程安全。

以及怎么"修改一个常量"(用什么引用技术)?

shared_ptr与STL

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
using namespace std;

class X
{
public:
	X() { cout << "X..." << endl; }
	~X() { cout << "~X..." << endl; }
};

int main()
{
	vector<auto_ptr<X>> v;
	auto_ptr<X> p(new X);

	v.push_back(p);

	return 0;
}

在这里插入图片描述

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>
using namespace std;

class X
{
public:
	X() { cout << "X..." << endl; }
	~X() { cout << "~X..." << endl; }
};

int main()
{
	//vector<auto_ptr<X>> v;
	//auto_ptr<X> p(new X);
	//v.push_back(p);

	vector<boost::shared_ptr<X>> vv;
	boost::shared_ptr<X> p(new X);
	vv.push_back(p);

	cout << p.use_count() << endl;
	return 0;
}

在这里插入图片描述

循环引用


#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;

class Parent;
class Child;

typedef boost::shared_ptr<Parent> parent_ptr;
typedef boost::shared_ptr<Child> child_ptr;

class Parent
{
public:
	Parent() { cout << "Parent..." << endl; }
	~Parent() { cout << "~Parent..." << endl; }
	child_ptr child_;
};

class Child
{
public:
	Child() { cout << "Child..." << endl; }
	~Child() { cout << "~Child..." << endl; }
	parent_ptr parent_;
};

int main()
{
	parent_ptr parent(new Parent);
	child_ptr child(new Child);

	parent->child_ = child;
	child->parent_ = parent;

	return 0;
}

运行结果发现没有调用析构函数。
在这里插入图片描述

解决办法(打破循环引用)

1 手动打破循环引用

#include <iostream>
#include <boost/shared_ptr.hpp>
using namespace std;

class Parent;
class Child;

typedef boost::shared_ptr<Parent> parent_ptr;
typedef boost::shared_ptr<Child> child_ptr;

class Parent
{
public:
	Parent() { cout << "Parent..." << endl; }
	~Parent() { cout << "~Parent..." << endl; }
	child_ptr child_;
};

class Child
{
public:
	Child() { cout << "Child..." << endl; }
	~Child() { cout << "~Child..." << endl; }
	parent_ptr parent_;
};

int main()
{
	parent_ptr parent(new Parent);
	child_ptr child(new Child);

	parent->child_ = child;
	child->parent_ = parent;

	parent->child_.reset();  // xxx
	return 0;
}

在这里插入图片描述

2 弱引用weak_ptr

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>	// xxx
using namespace std;

class Parent;
class Child;

typedef boost::shared_ptr<Parent> parent_ptr;
typedef boost::shared_ptr<Child> child_ptr;

class Parent
{
public:
	Parent() { cout << "Parent..." << endl; }
	~Parent() { cout << "~Parent..." << endl; }
	//child_ptr child_;
	boost::weak_ptr<Child> child_;	// xxx
};

class Child
{
public:
	Child() { cout << "Child..." << endl; }
	~Child() { cout << "~Child..." << endl; }
	parent_ptr parent_;
};

int main()
{
	parent_ptr parent(new Parent);
	child_ptr child(new Child);

	parent->child_ = child;
	child->parent_ = parent;

	//parent->child_.reset();  // xxx
	return 0;
}

在这里插入图片描述
注意事项:

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <vector>
using namespace std;

class X
{
public:
	X() { cout << "X..." << endl; }
	~X() { cout << "~X..." << endl; }

	void Fun() { cout << "Fun..." << endl; }
};

int main()
{
	boost::weak_ptr<X> p;
	{
		boost::shared_ptr<X> p2(new X);
		cout << p2.use_count() << endl;

		p = p2;
		cout << p2.use_count() << endl;

		boost::shared_ptr<X> p3 = p.lock();
		if (!p3)
			cout << "object is destroyed" << endl;
		else
			p3->Fun();
	}

	boost::shared_ptr<X> p4 = p.lock();
	if (!p4)
		cout << "object is destroyed" << endl;
	else
		p4->Fun();
	return 0;
}

在这里插入图片描述

scoped_array/shared_array

#include <iostream>
#include <boost/scoped_array.hpp>

using namespace std;

class X
{
public:
	X() { cout << "X..." << endl; }
	~X() { cout << "~X..." << endl; }
};

int main()
{
	boost::scoped_array<X> p(new X[3]);
	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值