Version: 3.3.0
wxSharedPtr< T > Class Template Reference

#include <wx/sharedptr.h>

+ Inheritance diagram for wxSharedPtr< T >:

Detailed Description

template<typename T>
class wxSharedPtr< T >

A smart pointer with non-intrusive reference counting.

It is modelled after boost::shared_ptr<> and can be used with STL containers and wxVector<T> unlike std::auto_ptr<> and wxScopedPtr<T>.

Library:  wxBase
Category:  Smart Pointers
See also
wxScopedPtr<T>, wxWeakRef<T>, wxObjectDataPtr<T>

Public Member Functions

 wxSharedPtr (T *ptr=nullptr)
 Constructor. More...
 
template<typename Deleter >
 wxSharedPtr (T *ptr, Deleter d)
 Constructor. More...
 
 wxSharedPtr (const wxSharedPtr< T > &tocopy)
 Copy constructor. More...
 
 ~wxSharedPtr ()
 Destructor. More...
 
T * get () const
 Returns pointer to its object or nullptr. More...
 
 operator unspecified_bool_type () const
 Conversion to a boolean expression (in a variant which is not convertible to anything but a boolean expression). More...
 
operator* () const
 Returns a reference to the object. More...
 
T * operator-> () const
 Smart pointer member access. More...
 
wxSharedPtr< T > & operator= (T *ptr)
 Assignment operator. More...
 
wxSharedPtr< T > & operator= (const wxSharedPtr< T > &tocopy)
 Assignment operator. More...
 
void reset (T *ptr=nullptr)
 Reset pointer to ptr. More...
 
template<typename Deleter >
void reset (T *ptr, Deleter d)
 Reset pointer to ptr. More...
 
bool unique () const
 Returns true if this is the only pointer pointing to its object. More...
 
long use_count () const
 Returns the number of pointers pointing to its object. More...
 

Constructor & Destructor Documentation

◆ wxSharedPtr() [1/3]

template<typename T >
wxSharedPtr< T >::wxSharedPtr ( T *  ptr = nullptr)
explicit

Constructor.

Creates shared pointer from the raw pointer ptr and takes ownership of it.

◆ wxSharedPtr() [2/3]

template<typename T >
template<typename Deleter >
wxSharedPtr< T >::wxSharedPtr ( T *  ptr,
Deleter  d 
)
explicit

Constructor.

Creates shared pointer from the raw pointer ptr and deleter d and takes ownership of it.

Parameters
ptrThe raw pointer.
dDeleter - a functor that is called instead of delete to free the ptr raw pointer when its reference count drops to zero.
Since
3.0

◆ wxSharedPtr() [3/3]

template<typename T >
wxSharedPtr< T >::wxSharedPtr ( const wxSharedPtr< T > &  tocopy)

Copy constructor.

◆ ~wxSharedPtr()

template<typename T >
wxSharedPtr< T >::~wxSharedPtr ( )

Destructor.

Member Function Documentation

◆ get()

template<typename T >
T* wxSharedPtr< T >::get ( ) const

Returns pointer to its object or nullptr.

◆ operator unspecified_bool_type()

template<typename T >
wxSharedPtr< T >::operator unspecified_bool_type ( ) const

Conversion to a boolean expression (in a variant which is not convertible to anything but a boolean expression).

If this class contains a valid pointer it will return true, if it contains a nullptr pointer it will return false.

◆ operator*()

template<typename T >
T wxSharedPtr< T >::operator* ( ) const

Returns a reference to the object.

If the internal pointer is nullptr this method will cause an assert in debug mode.

◆ operator->()

template<typename T >
T* wxSharedPtr< T >::operator-> ( ) const

Smart pointer member access.

Returns pointer to its object.

If the internal pointer is nullptr this method will cause an assert in debug mode.

◆ operator=() [1/2]

template<typename T >
wxSharedPtr<T>& wxSharedPtr< T >::operator= ( const wxSharedPtr< T > &  tocopy)

Assignment operator.

Releases any previously held pointer and creates a reference to the same object as topcopy.

◆ operator=() [2/2]

template<typename T >
wxSharedPtr<T>& wxSharedPtr< T >::operator= ( T *  ptr)

Assignment operator.

Releases any previously held pointer and creates a reference to ptr.

◆ reset() [1/2]

template<typename T >
template<typename Deleter >
void wxSharedPtr< T >::reset ( T *  ptr,
Deleter  d 
)

Reset pointer to ptr.

If the reference count of the previously owned pointer was 1 it will be deleted.

Parameters
ptrThe new raw pointer.
dDeleter - a functor that is called instead of delete to free the ptr raw pointer when its reference count drops to zero.
Since
3.0

◆ reset() [2/2]

template<typename T >
void wxSharedPtr< T >::reset ( T *  ptr = nullptr)

Reset pointer to ptr.

If the reference count of the previously owned pointer was 1 it will be deleted.

◆ unique()

template<typename T >
bool wxSharedPtr< T >::unique ( ) const

Returns true if this is the only pointer pointing to its object.

◆ use_count()

template<typename T >
long wxSharedPtr< T >::use_count ( ) const

Returns the number of pointers pointing to its object.