class FTestA { public: FTestA() { UE_LOG(LogTemp, Warning, TEXT("FTestA 构造")); } void TestFun() { UE_LOG(LogTemp, Warning, TEXT("FTestA TestFun方法")); } ~FTestA() { UE_LOG(LogTemp, Warning, TEXT("FTestA 析构")); } }; class FTestAChild : public FTestA { public: FTestAChild() { UE_LOG(LogTemp, Warning, TEXT("FTestAChild 构造")); } void TestFunChild() { UE_LOG(LogTemp, Warning, TEXT("FTestAChild TestFunChild方法")); } ~FTestAChild() { UE_LOG(LogTemp, Warning, TEXT("FTestAChild 析构")); } }; UFUNCTION(BlueprintCallable) void MyTest(); void MyTest() { //共享指针 { TSharedPtr<FTestA, ESPMode::NotThreadSafe> SharedPtr = MakeShareable(new FTestA); //创建时SharedReferenceCount=1 if (SharedPtr.IsValid()) { SharedPtr->TestFun(); int32 SharedReferenceCount = SharedPtr.GetSharedReferenceCount(); UE_LOG(LogTemp, Warning, TEXT("SharedReferenceCount : %d"), SharedReferenceCount); //手动释放 //SharedPtr = nullptr; UE_LOG(LogTemp, Warning, TEXT("SharedPtr.IsUnique : %d"), SharedPtr.IsUnique()); SharedPtr.Reset(); } } //共享引用 { //TSharedRef<FTestA, ESPMode::NotThreadSafe> SharedRef = MakeShareable(new FTestA()); //将共享指针隐式转换成引用 TSharedRef<FTestA, ESPMode::NotThreadSafe> SharedRef(new FTestA); SharedRef->TestFun(); int32 SharedReferenceCount = SharedRef.GetSharedReferenceCount(); UE_LOG(LogTemp, Warning, TEXT("SharedReferenceCount : %d"), SharedReferenceCount); UE_LOG(LogTemp, Warning, TEXT("SharedRef.IsUnique : %d"), SharedRef.IsUnique()); } //相互转化 { //共享引用转化成共享指针 UE_LOG(LogTemp, Warning, TEXT("共享引用转化成共享指针")); TSharedRef<FTestA, ESPMode::NotThreadSafe> SharedRef(new FTestA); TSharedPtr<FTestA, ESPMode::NotThreadSafe> SharedPtr = SharedRef; } { //共享指针转化成共享引用 UE_LOG(LogTemp, Warning, TEXT("共享指针转化成共享引用")); TSharedPtr<FTestA, ESPMode::NotThreadSafe> SharedPtr = MakeShareable(new FTestA); TSharedRef<FTestA, ESPMode::NotThreadSafe> SharedRef = SharedPtr.ToSharedRef(); } //继承关系转化 { TSharedPtr<FTestA, ESPMode::NotThreadSafe> SharedPtr = MakeShareable(new FTestAChild); if(SharedPtr.IsValid()) { SharedPtr->TestFun(); TSharedPtr<FTestAChild, ESPMode::NotThreadSafe> SharedPtr2 = StaticCastSharedPtr<FTestAChild>(SharedPtr); //ConstCastSharedPtr:常量共享指针转化 if (SharedPtr2.IsValid()) { SharedPtr2->TestFunChild(); } } } } //弱指针的用法: //1、弱指针不能影响强指针的引用计数 //2、在使用弱指针时,需要对其进行检测,判断其是否有效 //3、弱指针最大的用途就是解决环形引用的问题 //4、弱指针另一个作用是不对资源进行竞争,只能使用资源,却没有权限控制资源的生命周期 class IAnimal { public: virtual void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) = 0; virtual ~IAnimal() {}; }; class FMouse; class FElephant; class FLion : public IAnimal { public: FLion() { UE_LOG(LogTemp, Warning, TEXT("我是狮子,我克制老鼠")); } virtual void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) { Mouse = StaticCastSharedPtr<FMouse>(InWhatAnimal); } virtual ~FLion() { UE_LOG(LogTemp, Warning, TEXT("我是狮子,我没了")); } //TSharedPtr<FMouse> Mouse; TWeakPtr<FMouse> Mouse; }; class FMouse : public IAnimal { public: FMouse() { UE_LOG(LogTemp, Warning, TEXT("我是老鼠,我克制大象")); } virtual void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) { Elephant = StaticCastSharedPtr<FElephant>(InWhatAnimal); } virtual ~FMouse() { UE_LOG(LogTemp, Warning, TEXT("我是老鼠,我没了")); } //TSharedPtr<FElephant> Elephant; TWeakPtr<FElephant> Elephant; }; class FElephant : public IAnimal { public: FElephant() { UE_LOG(LogTemp, Warning, TEXT("我是大象,我克制狮子")); } virtual void SetRestraint(const TSharedPtr<IAnimal>& InWhatAnimal) { Lion = StaticCastSharedPtr<FLion>(InWhatAnimal); } virtual ~FElephant() { UE_LOG(LogTemp, Warning, TEXT("我是大象,我没了")); } //TSharedPtr<FLion> Lion; TWeakPtr<FLion> Lion; }; UFUNCTION(BlueprintCallable) void MyTest2(); void MyTest2() { TSharedPtr<FElephant> Elephant = MakeShareable(new FElephant); TSharedPtr<FLion> Lion = MakeShareable(new FLion); TSharedPtr<FMouse> Mouse = MakeShareable(new FMouse); Elephant->SetRestraint(Lion); Lion->SetRestraint(Mouse); Mouse->SetRestraint(Elephant); UE_LOG(LogTemp, Warning, TEXT("SharedReferenceCount : %d"), Elephant.GetSharedReferenceCount()); UE_LOG(LogTemp, Warning, TEXT("SharedReferenceCount : %d"), Lion.GetSharedReferenceCount()); UE_LOG(LogTemp, Warning, TEXT("SharedReferenceCount : %d"), Mouse.GetSharedReferenceCount()); }