You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
Updated engine version Changed font settings for multiple terms Changed all mentions of NULL to null to reflect changes made to the engine. [CL 2622022 by Matthew Clark in Main branch]
91 lines
2.9 KiB
Plaintext
91 lines
2.9 KiB
Plaintext
Availability:Public
|
|
Title:Shared Pointers
|
|
Crumbs:%ROOT%, Programming, Programming/UnrealArchitecture/SmartPointerLibrary
|
|
Description:Smart pointers that support shared ownership, automatic invalidation, weak references, and more.
|
|
Version: 4.9
|
|
|
|
[TOC(start:2)]
|
|
|
|
A **shared pointer** is a fancy type of non-intrusive, reference-counted smart pointer that supports both strong and weak references. Shared pointers inherently include all the benefits of basic smart pointers in that they prevent memory leaks, uninitialized memory, etc., but also bring additional features, such as:
|
|
|
|
* Shared ownership - reference counting
|
|
* Automatic invalidation - volatile objects can be safely referenced
|
|
* Weak references - allows avoidance of cycles via [](Programming/UnrealArchitecture/SmartPointerLibrary/WeakPointer)
|
|
* Improved indication of programmer intent - distinguishes owners from observers and provides non-nullable references ( [](Programming/UnrealArchitecture/SmartPointerLibrary/SharedReference) )
|
|
|
|
|
|
Some other basic characteristics of shared pointers:
|
|
|
|
* Very robust syntax
|
|
* Non-intrusive (but reflection is possible)
|
|
* Thread-safe (conditionally)
|
|
* Good performance, light on memory
|
|
|
|
|
|
## Declaration and Initialization
|
|
|
|
Shared references are always initialized with a valid object and cannot be declared without being initialized.
|
|
|
|
Examples of creating new shared pointers:
|
|
|
|
// Create an empty shared pointer
|
|
TSharedPtr<FTreeNode> EmptyNode;
|
|
|
|
// Create a shared pointer to a new object
|
|
TSharedPtr<FTreeNode> Node( new FTreeNode() );
|
|
|
|
In the second example, the new FTreeNode object is actually owned by the shared pointer pointing to it. When the shared pointer goes out of scope, the object is destroyed.
|
|
|
|
When you copy a shared pointer, a reference to the object being pointed to is added.
|
|
|
|
TSharedPtr<FTreeNode> OtherNodeRef = Node;
|
|
|
|
The object will persist until no more shared pointers are referencing it.
|
|
|
|
Shared pointers can be reset using the **Reset()** method or by assigning null.
|
|
|
|
Node.Reset();
|
|
Node = null;
|
|
|
|
## Dereferencing and Accessing
|
|
|
|
Dereferencing, calling methods, and accessing members is done in the same manner you would with regular C++ pointers.
|
|
|
|
Node->ListChildren();
|
|
Node.Get()->ListChildren();
|
|
( *Node ).ListChildren();
|
|
|
|
## Comparison
|
|
|
|
Shared pointers can be tested for equality, i.e. whether they point to the same object.
|
|
|
|
TSharedPtr<FTreeNode NodeA, NodeB;
|
|
|
|
if( NodeA == NodeB )
|
|
{
|
|
...
|
|
}
|
|
|
|
Shared pointers can be tested for null by using the **IsValid()** method or accessing the raw pointer and performing a comparison.
|
|
|
|
if( Node.IsValid() )
|
|
{
|
|
...
|
|
}
|
|
|
|
if( Node.Get() != null )
|
|
{
|
|
...
|
|
}
|
|
|
|
## Conversion
|
|
|
|
Implicit conversion is not allowed as it is extremely dangerous. For example, the following will not compile:
|
|
|
|
TSharedPtr<float> Height = 45.0f;
|
|
|
|
However, the MakeShareable() method can be used to be explicit.
|
|
|
|
TSharedPtr<float> Height = MakeShareable( new float( 45.0f ) );
|
|
|