MakeShareable()/MakeShared<T>()
函数
Reset()
函数
class SimpleObject {
public:
SimpleObject() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"SimpleObject Construct")); }
~SimpleObject() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"SimpleObject Destruct")); }
void ExeFun() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"Execute")); }
};
// 快速创建共享指针
TSharedPtr<SimpleObject> simObjectPtr(new SimpleObject());
// MakeShareable 创建共享指针
TSharedPtr<SimpleObject> simObjectPtr2 = MakeShareable(new SimpleObject());
// 创建线程安全
TSharedPtr<SimpleObject, ESPMode::ThreadSafe> simObjectPtr3 = MakeShareable(new SimpleObject());
// 查看引用计数
UE_LOG(LogTemp, Warning,
TEXT(__FUNCTION__"引用计数: simObjectPtr[%d], simObjectPtr2[%d], simObjectPtr3[%d] "),
simObjectPtr.GetSharedReferenceCount(), simObjectPtr2.GetSharedReferenceCount(), simObjectPtr3.GetSharedReferenceCount());
// 重置共享指针
simObjectPtr.Reset();
simObjectPtr2 = nullptr;
赋值
MoveTemp / MoveTempIfPossible
// 复制共享指针
TSharedPtr<SimpleObject> simObjectPtr_copy = simObjectPtr;
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"引用计数: simObjectPtr[%d], simObjectPtr_copy[%d],"),
simObjectPtr.GetSharedReferenceCount(), simObjectPtr_copy.GetSharedReferenceCount());
// 转移共享指针
TSharedPtr<SimpleObject> simObjectPtr_MoveTemp = MoveTemp(simObjectPtr_copy); // 另 MoveTempIfPossible()
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"引用计数: simObjectPtr[%d], simObjectPtr_copy[%d], simObjectPtr_MoveTemp[%d]"),
simObjectPtr.GetSharedReferenceCount(), simObjectPtr_copy.GetSharedReferenceCount(), simObjectPtr_MoveTemp.GetSharedReferenceCount()
->
运算符
Get()
函数
IsValid()
函数
==
!=
运算符
if (simObjectPtr) // 条件判断
{
simObjectPtr->ExeFun(); // 解引用
}
if (simObjectPtr.Get() != nullptr) // 条件判断
{
simObjectPtr.Get()->ExeFun(); //解引用
}
if (simObjectPtr.IsValid()) // 条件判断
{
(*simObjectPtr).ExeFun(); // 解引用
}
if (simObjectPtr == simObjectPtr_copy) // 对比
{
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"simObjectPtr_copy == simObjectPtr"));
}
MakeShareable()/MakeShared<T>()
函数
// 创建共享引用
TSharedRef<SimpleObject> objRef(new SimpleObject());
TSharedRef<SimpleObject> objRef2 = MakeShareable(new SimpleObject());
TSharedRef<SimpleObject> objRef3 = MakeShared<SimpleObject>();
隐式转化
ToSharedRef()
// TSharedRef -> TSharedPtr
TSharedPtr<SimpleObject> objPtr = objRef;
// TSharedPtr -> TSharedRef
objRef3= objPtr.ToSharedRef();
没有 IsValid() 函数
// 共享指针比较
if (objRef == objRef3)
{
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"objRef == objRef3 , 引用计数:%d"), objPtr.GetSharedReferenceCount());
}
通过 TSharedPtr 创建
通过 TSharedRef 创建
运算符 =
赋值
IsValid()
函数判断有效性
Pin()
函数转成 TSharedPtr ,再解引用访问对象
Reset()
或 nullptr
重置
// 强指针创建弱指针
TSharedPtr<SimpleObject> ObjPtr=MakeShared<SimpleObject>();
TWeakPtr<SimpleObject> ObjWeakPtr(ObjPtr);
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"step1 引用计数:ObjPtr[%d]"), ObjPtr.GetSharedReferenceCount());
//强引用创建弱指针
TSharedRef<SimpleObject> objRef = MakeShareable(new SimpleObject());
TWeakPtr<SimpleObject> ObjWeakPtr2(objRef);
TWeakPtr<SimpleObject> ObjWeakPtr_Copy = ObjWeakPtr;
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"step2 引用计数:ObjPtr[%d]"), ObjPtr.GetSharedReferenceCount());
// 判断有效性
if (ObjWeakPtr.IsValid())
{
TSharedPtr<SimpleObject> ObjPtr2 = ObjWeakPtr.Pin();
ObjPtr2->ExeFun();
}
// 清空强指针
ObjPtr.Reset();
TSharedPtr<SimpleObject> ObjPtr2 = ObjWeakPtr.Pin();
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"step3 引用计数:ObjPtr[%d]"), ObjPtr.GetSharedReferenceCount());
// 判断有效性
if (!ObjPtr2)
{
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"弱指针已空 "));
}
// 重置
ObjWeakPtr.Reset();
ObjWeakPtr_Copy = nullptr;
MakeUnique
IsValid()
->
运算符
Get()
函数
Release()
释放并返回指针
Reset()
或 nullptr
重置
// 创建唯一指针
TUniquePtr<SimpleObject> ObjUniquePtr = MakeUnique<SimpleObject>();
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Validity: ObjUniquePtr[%d]"), ObjUniquePtr.IsValid());
// 判断有效性
if (ObjUniquePtr.IsValid())
{
ObjUniquePtr->ExeFun(); // 解引用
}
// 释放指针,移交
TUniquePtr<SimpleObject> ObjUniquePtr2(ObjUniquePtr.Release());
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Validity: ObjUniquePtr[%d], ObjUniquePtr2[%d]"), ObjUniquePtr.IsValid(), ObjUniquePtr2.IsValid());
// 重置
ObjUniquePtr.Reset();
ObjUniquePtr2 = nullptr;
派生类转基类 隐式转换
基类转派生类 StaticCastSharedPtr
非常量转常量 ConstCastSharedPtr
TSharedPtr<SimpleObject> simpleObj;
TSharedPtr<ComplexObject> complexObj = MakeShared<ComplexObject>();
// 派生类转基类
simpleObj = complexObj;
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"simpleObj is %s"), simpleObj.IsValid() ? TEXT("Valid") : TEXT("Not Valid"));
// 基类转派生类
TSharedPtr<ComplexObject> complexObj2 = StaticCastSharedPtr<ComplexObject>(simpleObj);
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"complexObj2 is %s"), complexObj2.IsValid() ? TEXT("Valid") : TEXT("Not Valid"));
// 常量指针转非常量指针
const TSharedPtr<SimpleObject> simpleObj_const(new SimpleObject());
TSharedPtr<SimpleObject> simpleObj_mutable = ConstCastSharedPtr<SimpleObject>(simpleObj_const);
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"simpleObj_mutable is %s"), simpleObj_mutable.IsValid() ? TEXT("Valid") : TEXT("Not Valid"));
// 创建唯一指针
TUniquePtr
UE_LOG(LogTemp, Warning, TEXT(FUNCTION" Validity: ObjUniquePtr[%d]"), ObjUniquePtr.IsValid());
// 判断有效性
if (ObjUniquePtr.IsValid())
{
ObjUniquePtr->ExeFun(); // 解引用
}
// 释放指针,移交
TUniquePtr<SimpleObject> ObjUniquePtr2(ObjUniquePtr.Release());
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Validity: ObjUniquePtr[%d], ObjUniquePtr2[%d]"), ObjUniquePtr.IsValid(), ObjUniquePtr2.IsValid());
// 重置
ObjUniquePtr.Reset();
ObjUniquePtr2 = nullptr;
代码省略
自定义类继承 TSharedFromThis 模板类
TSharedFromThis 会保存一个弱指针
AsShared()
将裸指针转智共享引用,可再隐式转为共享指针
SharedThis(this)
会返回具备"this"类型的TSharedRef
不要在构造函数中调用 AsShared 或 Shared,共享引用此时并未初始化,将导致崩溃或断言
// 基类
class BaseClass :public TSharedFromThis<BaseClass>
{
public:
BaseClass() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__)); }
virtual ~BaseClass() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__)); }
virtual void ExeFun() {
TSharedRef<BaseClass> ThisAsSharedRef = AsShared();
}
};
// 派生类
class ChildClass :public BaseClass
{
public:
ChildClass() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__)); }
virtual ~ChildClass() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__)); }
virtual void ExeFun() override{
//AsShared()返回 TSharedRef<BaseClass>, 因而编译不通过
//TSharedRef<ChildClass> AsSharedRef = AsShared();
TSharedRef<ChildClass> AsSharedRef = SharedThis(this);
}
};
TSharedPtr<BaseClass> BaseClassPtr = MakeShared<BaseClass>();
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" 引用计数:BaseClassPtr[%d]"), BaseClassPtr.GetSharedReferenceCount());
BaseClass* tempPtr = BaseClassPtr.Get();
TSharedPtr<BaseClass> BaseClassPtr_Shared =tempPtr->AsShared();
UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" 引用计数:BaseClassPtr[%d], BaseClassPtr_Shared[%d]"),
BaseClassPtr.GetSharedReferenceCount(), BaseClassPtr_Shared.GetSharedReferenceCount());
// 使用下面语句运行,程序死机
// TSharedPtr<BaseClass> BaseClassPtr_New = MakeShareable(tempPtr);
【UE4 C++ 基础知识】<15> 智能指针 TSharedPtr、UniquePtr、TWeakPtr、TSharedRef
原文:https://www.cnblogs.com/shiroe/p/14729821.html