C++ 补充 & C++ 11 - C++智能指针weak_ptr 使用 (自从C++11)

weak_ptr 使用 (自从C++11)

weak_ptr 设计的目的是为配合 shared_ptr 而引入的一种智能指针来协助 shared_ptr 工作, 它只可以从一个 shared_ptr 或另一个 weak_ptr 对象构造, 它的构造和析构不会引起引用记数的增加或减少. 同时weak_ptr 没有重载*和->但可以使用 lock 获得一个可用的 shared_ptr 对象。
在这里插入图片描述
测试代码:

#include <stdio.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>

using namespace std;

class girl;

class boy
{
public:
	boy()
	{
		cout << "boy construct!" << endl;
	}

	~boy()
	{
		cout << "boy destruct!" << endl;
	}

	void set_girl_friend(shared_ptr<girl>& g)
	{
		this->girl_friend = g;
	}

private:
	shared_ptr<girl> girl_friend;
};

class girl
{
public:
	girl()
	{
		cout << "girl construct !" << endl;
	}

	~girl()
	{
		cout << "girl destruct!" << endl;
	}

	void set_boy_friend(shared_ptr<boy>& b)
	{
		this->boy_friend = b;
	}

private:
	shared_ptr<boy> boy_friend;
};

void use_trap()
{
	shared_ptr<girl> sp_girl(new girl()); /* 白娘子 */
	shared_ptr<boy> sp_boy(new boy()); /* 许仙 */

	sp_girl->set_boy_friend(sp_boy);
	sp_boy->set_girl_friend(sp_girl);

}


int main()
{
	use_trap();

	system("pause");
	return 0;
}

执行:
在这里插入图片描述
修改测试代码:

#include <stdio.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>

using namespace std;

class girl;

class boy
{
public:
	boy()
	{
		cout << "boy construct!" << endl;
	}

	~boy()
	{
		cout << "boy destruct!" << endl;
	}

	void set_girl_friend(shared_ptr<girl>& g)
	{
		this->girl_friend = g;
	}

private:
	shared_ptr<girl> girl_friend;
};

class girl
{
public:
	girl()
	{
		cout << "girl construct !" << endl;
	}

	~girl()
	{
		cout << "girl destruct!" << endl;
	}

	void set_boy_friend(shared_ptr<boy>& b)
	{
		this->boy_friend = b;
	}

private:
	shared_ptr<boy> boy_friend;
};

void use_trap()
{
	shared_ptr<girl> sp_girl(new girl()); /* 白娘子 */
	shared_ptr<boy> sp_boy(new boy()); /* 许仙 */

	sp_girl->set_boy_friend(sp_boy);
	//sp_boy->set_girl_friend(sp_girl);

}


int main()
{
	use_trap();

	system("pause");
	return 0;
}

画图理解:
在这里插入图片描述
老师的讲解:
在这里插入图片描述

在这里插入图片描述
执行修改的测试代码
在这里插入图片描述

demo

#include <stdio.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>

using namespace std;

class girl;

class boy
{
public:
	boy()
	{
		cout << "boy construct!" << endl;
	}

	~boy()
	{
		cout << "boy destruct!" << endl;
	}

	void set_girl_friend(shared_ptr<girl>& g)
	{
		this->girl_friend = g;
	}

private:
	shared_ptr<girl> girl_friend;
};

class girl
{
public:
	girl()
	{
		cout << "girl construct !" << endl;
	}

	~girl()
	{
		cout << "girl destruct!" << endl;
	}

	void set_boy_friend(shared_ptr<boy>& b)
	{
		this->boy_friend = b;
	}

private:
	shared_ptr<boy> boy_friend;
};

void use_trap()
{
	shared_ptr<girl> sp_girl(new girl()); /* 白娘子 */
	shared_ptr<boy> sp_boy(new boy()); /* 许仙 */

	/* 弱指针的使用 */
	weak_ptr<girl> wp_gir1; /* 定义空的弱指针 */
	weak_ptr<girl> wp_gir2(sp_girl); /* 使用共享构造 */
	wp_gir1 = sp_girl; /* 允许共享指针赋值给弱指针 */

	cout << "sp_girl ref_count: " << sp_girl.use_count() << endl;
	cout << "wp_girl.use_count: " << wp_gir1.use_count() << endl;

	//(*sp_girl).set_boy_friend(sp_boy); 弱指针不支持* 和 -> 对指针访问
	//sp_girl->set_boy_friend(sp_boy);

	// 在必要的时候可以转成共享指针
	shared_ptr<girl> sp_girl1;
	sp_girl1 = wp_gir1.lock();
	cout << " after lock wp_girl.use_count: " << wp_gir1.use_count() << endl;

	sp_girl->set_boy_friend(sp_boy);
	//sp_boy->set_girl_friend(sp_girl);

}


int main()
{
	use_trap();

	system("pause");
	return 0;
}

执行:

在这里插入图片描述

结语:

弱指针主要是为了解决, 交叉引用

时间: 2020-07-04

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值