#include "Engine.h" #include "MyObject.h" TSharedPtrMyObjectSharedPtr = MakeShareable(new MyObject()); TSharedRef MyObjectSharedRef = MyObjectSharedPtr.ToSharedRef(); MyObject* RawPtr = MyObjectSharedRef.Get();
#include "CoreMinimal.h" #include "MyActor.h" void MyActor::MyFunction(TWeakPtrThis example is from an Unreal Engine 4 class implementation file for an actor called MyActor. The function MyFunction takes a weak pointer to a MyActor instance as an argument. The TWeakPtr Pin function is first called to temporarily convert the weak pointer to a shared pointer, which is then converted to a shared reference using the TSharedPtr ToSharedRef function. Finally, the TSharedRef Get function is used to get a raw pointer to the instance, before ending the function. In both examples, the TSharedRef Get function is used to get a pointer to the object referred to by the shared reference. The TSharedRef class is a part of Unreal Engine's core module.WeakPtr) { if (!WeakPtr.IsValid()) { return; } TSharedPtr SharedPtr = WeakPtr.Pin(); TSharedRef SharedRef = SharedPtr.ToSharedRef(); MyActor* RawPtr = SharedRef.Get(); }