Compartilhar via


ComPtr Class

Creates a smart pointer type that represents the interface specified by the template parameter. ComPtr automatically maintains a reference count for the underlying interface pointer and releases the interface when the reference count goes to zero.

template <
   typename T
>
class ComPtr;

template<
   class U
>
friend class ComPtr;

Parameters

  • T
    The interface that the ComPtr represents.

  • U
    A class to which the current ComPtr is a friend. (The template that uses this parameter is protected.)

Remarks

ComPtr<> declares a type that represents the underlying interface pointer. Use ComPtr<> to declare a variable and then use the arrow member-access operator (->) to access an interface member function.

For more information about smart pointers, see the "COM Smart Pointers" subsection of the COM Coding Practices topic in the MSDN Library.

Members

Public Typedefs

Name

Description

InterfaceType

A synonym for the type specified by the T template parameter.

Public Constructors

Name

Description

ComPtr::ComPtr Constructor

Intializes a new instance of the ComPtr class. Overloads provide default, copy, move, and conversion constructors.

ComPtr::~ComPtr Destructor

Deinitializes an instance of ComPtr.

Public Methods

Name

Description

ComPtr::As Method

Returns a ComPtr object that represents the interface identified by the specified template parameter.

ComPtr::AsIID Method

Returns a ComPtr object that represents the interface identified by the specified interface ID.

ComPtr::AsWeak Method

Retrieves a weak reference to the current object.

ComPtr::Attach Method

Associates this ComPtr with the interface type specified by the current template type parameter.

ComPtr::CopyTo Method

Copies the current or specified interface associated with this ComPtr to the specified output pointer.

ComPtr::Detach Method

Disassociates this ComPtr from the interface that it represents.

ComPtr::Get Method

Retrieves a pointer to the interface that is associated with this ComPtr.

ComPtr::GetAddressOf Method

Retrieves the address of the ptr_ data member, which contains a pointer to the interface represented by this ComPtr.

ComPtr::ReleaseAndGetAddressOf Method

Releases the interface associated with this ComPtr and then retrieves the address of the ptr_ data member, which contains a pointer to the interface that was released.

ComPtr::Swap Method

Exchanges the interface managed by the current ComPtr with the interface managed by the specified ComPtr.

Protected Methods

Name

Description

ComPtr::InternalAddRef Method

Increments the reference count of the interface associated with this ComPtr.

ComPtr::InternalRelease Method

Performs a COM Release operation on the interface associated with this ComPtr.

Public Operators

Name

Description

ComPtr::operator Microsoft::WRL::Details::BoolType Operator

Indicates whether or not a ComPtr is managing the object lifetime of an interface.

ComPtr::operator& Operator

Retrieves the address of the current ComPtr.

ComPtr::operator= Operator

Assigns a value to the current ComPtr.

ComPtr::operator-> Operator

Retrieves a pointer to the type specified by the current template parameter.

ComPtr::operator== Operator

Indicates whether two ComPtr objects are equal.

ComPtr::operator!= Operator

Indicates whether two ComPtr objects are not equal.

Protected Data Members

Name

Description

ComPtr::ptr_ Data Member

Contains a pointer to the interface that is associated with, and managed by this ComPtr.

Inheritance Hierarchy

ComPtr

Requirements

Header: client.h

Namespace: Microsoft::WRL

See Also

Reference

Microsoft::WRL Namespace