Platform::WeakReference Class

Represents a weak reference to an instance of a ref class.

Syntax

class WeakReference  

Parameters

Members

Constructors

Member Description
WeakReference::WeakReference Initializes a new instance of the WeakReference class.

Methods

Member Description
WeakReference::Resolve Returns a handle to the underlying ref class, or nullptr if the object no longer exists.

Operators

Member Description
WeakReference::operator= Assigns a new value to the WeakReference object.
WeakReference::operator BoolType Implements the safe bool pattern.

Remarks

The WeakReference class itself is not a ref class and therefore it does not inherit from Platform::Object^ and cannot be used in the signature of a public method.

WeakReference::operator=

Assigns a value to a WeakReference.

Syntax

WeakReference& operator=(decltype(__nullptr));    
WeakReference& operator=(const WeakReference& otherArg);   
WeakReference& operator=(WeakReference&& otherArg);    
WeakReference& operator=(const volatile ::Platform::Object^ const otherArg); 

Remarks

The last overload in the list above enables you to assign a ref class to a WeakReference variable. In this case the ref class is downcast to Platform::Object^. You restore the original type later by specifying it as the argument for the type parameter in the WeakReference::Resolve<T> member function.

WeakReference::operator BoolType

Implements the safe bool pattern for the WeakReference class. Not to be called explicitly from your code.

Syntax

BoolType BoolType()  

WeakReference::Resolve Method (Platform namespace)

Returns a handle to the original ref class, or nullptr if the object no longer exists.

Syntax


template<typename T>  
T^ Resolve() const  

Parameters

Property Value/Return Value

A handle to the ref class that the WeakReference object was previously associated with, or nullptr.

Example

This is the description for a Code Example.


Bar^ bar = ref new Bar();  
//use bar...  

if (bar != nullptr)  
{  
    WeakReference wr(bar);  
    Bar^ newReference = wr.Resolve<Bar>();  
}  

Note that the type parameter is T, not T^.

WeakReference::WeakReference Constructor

Provides various ways to construct a WeakReference.

Syntax

WeakReference();  
WeakReference(decltype(__nullptr));  
WeakReference(const WeakReference& otherArg);  
WeakReference(WeakReference&& otherArg);  
explicit WeakReference(const volatile ::Platform::Object^ const otherArg);  

Example

MyClass^ mc = ref new MyClass();  
WeakReference wr(mc);  
MyClass^ copy2 = wr.Resolve<MyClass>();    

See Also

Platform namespace