UE4_智能指针(2)

弱指针
  1. 弱指针不能影响强指针的引用计数
  2. 在使用弱指针时,需要对其进行检测,判断是否有效
  3. 弱指针最大的用途就是解决闭环引用问题
  4. 只是使用它,没有管理引用对象生命周期的权限
class IAnimal
{
public:
	// 克制
	virtual void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) = 0;
	// 类包含虚函数,但其不常用的析构函数不是虚函数;该类的实例可能无法进行正确析构
	virtual ~IAnimal(){}
	
};

class FLion;
class FMouse;

class FElephant : public IAnimal
{
public:
	FElephant()
	{
		UE_LOG(LogTemp, Warning, TEXT("我是大象,我克制狮子"));
	}
	~FElephant()
	{
		UE_LOG(LogTemp, Warning, TEXT("大象:啊~ 我没了"));
	}
	void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) override
	{
		Lion = StaticCastSharedPtr<FLion>(InWhatAnimal);
	}
public:
	TWeakPtr<FLion> Lion;

};

class FMouse : public IAnimal
{
public:
	FMouse()
	{
		UE_LOG(LogTemp,Warning,TEXT("我是老鼠,我克制大象"));
	}
	~FMouse()
	{
		UE_LOG(LogTemp,Warning,TEXT("老鼠:啊~ 我没了"));
	}
public:
	void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) override
	{
		Elephant = StaticCastSharedPtr<FElephant>(InWhatAnimal);
	}
public:
	TWeakPtr<FElephant> Elephant;

};

class FLion : public IAnimal
{
public:
	FLion()
	{
		UE_LOG(LogTemp, Warning, TEXT("我是狮子,我克制老鼠"));
	}
	~FLion()
	{
		UE_LOG(LogTemp, Warning, TEXT("狮子:啊~ 我没了"));
	}
public:
	void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) override
	{
		Mouse = StaticCastSharedPtr<FMouse>(InWhatAnimal);
	}
public:
	TWeakPtr<FMouse> Mouse;
};
{
	// 弱指针,可以解决闭环引用问题
	// 弱指针,只是使用它,没法管理引用对象的生命周期
	TSharedPtr<FElephant> Elephant = MakeShareable(new FElephant);  // 大象 引用计数=1
	TSharedPtr<FLion> Lion = MakeShareable(new FLion);				// 狮子 引用计数=1
	TSharedPtr<FMouse> Mouse = MakeShareable(new FMouse);           // 老鼠 引用计数=1
	
	Elephant->SetRestraint(Lion);         // 狮子引用计数变为2             
	Lion->SetRestraint(Mouse);			  // 老鼠引用计数变为2
	Mouse->SetRestraint(Elephant);        // 大象引用计数变为2
	
	int32 ElephantCount = 0;
	int32 LionCount = 0;
	int32 MouseCount = 0;
	ElephantCount= Elephant.GetSharedReferenceCount();
	LionCount = Lion.GetSharedReferenceCount();
	MouseCount = Mouse.GetSharedReferenceCount();
	UE_LOG(LogTemp,Warning,TEXT("引用计数:Elephant=%d\n LionCount=%d\n 	 MouseCount=%d\n"),ElephantCount,LionCount,MouseCount);
}
	
弱指针转共享指针
TSharedPtr<FElephant> Elephant = MakeShareable(new FElephant);  // 大象 引用计数=1
TWeakPtr<FElephant> Elephant_weakPtr(Elephant)
TSharedPtr<FElephant> Elephant2(Elephant_weakPtr.Pin());
TSharedFromThis
  1. TSharedFromThis 是一个模板类,普通c++类继承该类后,类将会保存一个弱指针,当通过智能指针取到该类的普通指针时,
    可以通过调用AsShared方法获取到该类的智能指针。
  2. 提供AsShared方法获取共享指针, 凡是派生自TSharedFromThis的对象请通过AsShared获取智能指针, 可以保证引用计数器的一致.
class FAnimal: TSharedFromThis<FAnimal>
{}

TSharedPtr<FAnimal> TestPtr = MakeShareable(new FAnimal());
FAnimal* TestNormalPtr = TestPtr.Get();
TSharedPtr<FAnimal> TestPtr2 = TestNormalPtr->AsShared();

只有通过智能指针获取的普通指针才能使用AsShared转换成智能指针,否则可能崩溃。因为通过MakeShareable创建智能指针时,才会初始化其弱指针。

TUniquePtr

唯一指针指向的对象只能被唯一指向,所以Unique指针不能赋值给其它指针。

TUniquePtr<FAnimal> StructPtr = MakeUnique<FAnimal>(new FAnimal());
普通指针转成智能指针
// 将普通指针转换成智能指针
FResAWM* Animal= new FAnimal;
// TSharedPtr<FAnimal> SharedResAWM = Animal->AsShared();
// FAnimal的析构函数必须实现,不然这样子写会报错
TSharedPtr<FAnimal> SharedResAW = MakeShareable(Animal);
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值