参考:https://docs.unrealengine.com/4.27/zh-CN/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/SmartPointerLibrary/WeakPointer/

定义

弱指针 存储对象的弱引用。与 共享指针共享引用 不同,弱指针不会阻止其引用的对象被销毁。

作用

弱指针的使用有助于授予意图——弱指针表明对引用对象的观察,而无需所有权,同时不控制其生命周期。

应用:打破引用循环

两个或多个对象使用智能指针保持彼此间的强引用时,将出现引用循环。在此类情况下,对象间会相互保护以免被删除。各对象固定被另一对象引用,因此对象无法在另一对象存在时被删除。如外部对象未对引用循环中对象进行引用,其实际上将出现泄漏。弱指针不会保留自身引用的对象,因此其可中断此类引用循环。要在未拥有对象时对其进行引用,并延长其寿命时,可使用软指针。

演示代码

演示斗兽棋鼠、象、狮子的相互克制关系,注意只需把一个成员的指针类型改成弱指针,就可以打破引用循环。
头文件:

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "UObject/NoExportTypes.h"
  5. #include "testWeakptrO.generated.h"
  6. /**
  7. *
  8. */
  9. UCLASS(Blueprintable)
  10. class TESTCPP_API UtestWeakptrO : public UObject
  11. {
  12. GENERATED_BODY()
  13. public:
  14. UFUNCTION(BlueprintCallable)
  15. void testWeakptr();
  16. };
  17. class IAnimal {
  18. public:
  19. virtual void SetRestraint(const TSharedPtr<IAnimal> &InWhatAnimal) = 0;
  20. virtual ~IAnimal(){}
  21. };
  22. class Fmouse :public IAnimal {
  23. public:
  24. //只需把一个成员的指针类型改成弱指针,就可以中断引用循环
  25. //TSharedPtr<Fmouse> elephant;
  26. TWeakPtr<Fmouse> elephant;
  27. Fmouse() { UE_LOG(LogTemp, Warning, TEXT("mouse atk elephant")); }
  28. virtual void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) { elephant = StaticCastSharedPtr<Fmouse>(InWhatAnimal); }
  29. virtual ~Fmouse() { UE_LOG(LogTemp, Warning, TEXT("mouse is dead")); }
  30. };
  31. class Flion :public IAnimal {
  32. public:
  33. TSharedPtr<Flion> mouse;
  34. Flion() { UE_LOG(LogTemp, Warning, TEXT("lion atk mouse")); }
  35. virtual void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) { mouse = StaticCastSharedPtr<Flion>(InWhatAnimal); }
  36. virtual ~Flion() { UE_LOG(LogTemp, Warning, TEXT("lion is dead")); }
  37. };
  38. class Felepant:public IAnimal{
  39. public:
  40. TSharedPtr<Felepant> lion;
  41. Felepant(){UE_LOG(LogTemp, Warning, TEXT("elephant atk lion")); }
  42. virtual void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) { lion = StaticCastSharedPtr<Felepant>(InWhatAnimal); }
  43. virtual ~Felepant(){ UE_LOG(LogTemp, Warning, TEXT("elephant is dead")); }
  44. };

实现:

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "testWeakptrO.h"
  3. void UtestWeakptrO::testWeakptr()
  4. { //构造3个动物类的ptr对象
  5. TSharedPtr<Felepant> myobjEle = MakeShareable(new Felepant);//ptrNum count+1
  6. TSharedPtr<Fmouse>myobjMou = MakeShareable(new Fmouse);//ptrNum count+1
  7. TSharedPtr<Flion>myobjFli = MakeShareable(new Flion);//ptrNum count+1
  8. //获取计数
  9. int32 EleCount = myobjEle.GetSharedReferenceCount();
  10. int32 MouCount = myobjMou.GetSharedReferenceCount();
  11. int32 FliCount = myobjFli.GetSharedReferenceCount();
  12. UE_LOG(LogTemp, Warning, TEXT("GetSharedReferenceCount:ElephantCount=%d\n LionCount=%d\n MouseCount=%d\n"),EleCount,MouCount,FliCount);
  13. }

image.png