Share via


WeakRef Class

Represents a weak reference that can be used by only the Windows Runtime, not classic COM. A weak reference represents an object that might or might not be accessible.

class WeakRef : public ComPtr<IWeakReference>

Remarks

A WeakRef object maintains a strong reference, which is associated with an object, and can be valid or invalid. Call the As() or AsIID() method to obtain a strong reference. When the strong reference is valid, it can access the associated object. When the strong reference is invalid (nullptr), the associated object is inaccessible.

A WeakRef object is typically used to represent an object whose existence is controlled by an external thread or application. For example, construct a WeakRef object from a reference to a file object. While the file is open, the strong reference is valid. But if the file is closed, the strong reference becomes invalid.

Members

Public Constructors

Name

Description

WeakRef::WeakRef Constructor

Initializes a new instance of the WeakRef class.

WeakRef::~WeakRef Destructor

Deinitializes the current instance of the WeakRef class.

Public Methods

Name

Description

WeakRef::As Method

Sets the specified ComPtr pointer parameter to represent the specified interface.

WeakRef::AsIID Method

Sets the specified ComPtr pointer parameter to represent the specified interface ID.

WeakRef::CopyTo Method

Assigns a pointer to an interface, if available, to the specified pointer variable.

Public Operators

Name

Description

WeakRef::operator& Operator

Returns a ComPtrRef object that represents the current WeakRef object.

Inheritance Hierarchy

ComPtr

WeakRef

Requirements

Header: client.h

Namespace: Microsoft::WRL

See Also

Reference

Microsoft::WRL Namespace