| Version: 3.2.5

Public Types | Public Member Functions | List of all members
wxWeakRef< T > Class Template Reference
Class List by Category » Smart Pointers

`#include <wx/weakref.h>`

Inheritance diagram for wxWeakRef< T >:
[legend]

## Detailed Description

### template<typename T>
class wxWeakRef< T >

wxWeakRef<T> is a template class for weak references to wxWidgets objects, such as wxEvtHandler, wxWindow and wxObject.

A weak reference behaves much like an ordinary pointer, but when the object pointed is destroyed, the weak reference is automatically reset to a NULL pointer.

wxWeakRef<T> can be used whenever one must keep a pointer to an object that one does not directly own, and that may be destroyed before the object holding the reference.

wxWeakRef<T> is a small object and the mechanism behind it is fast (O(1)). So the overall cost of using it is small.

Example:

wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" );
wxWeakRef<wxWindow> wr = wnd;
wxWindowRef wr2 = wnd; // Same as above, but using a typedef
// Do things with window
wnd->Show( true );
// Weak ref is used like an ordinary pointer
wr->Show( false );
wnd->Destroy();
// Now the weak ref has been reset, so we don't risk accessing
// a dangling pointer:
wxASSERT( wr==NULL );
wxWindow
wxWindow is the base class for all windows and represents any visible object on screen.
Definition: window.h:346
wxWindow::Destroy
virtual bool Destroy()
Destroys the window safely.
wxWindow::Show
virtual bool Show(bool show=true)
Shows or hides the window.
wxID_ANY
@ wxID_ANY
Any id: means that we don't care about the id, whether when installing an event handler or when creat...
Definition: defs.h:597
wxASSERT
#define wxASSERT(condition)
Assert macro.
Definition: debug.h:75

wxWeakRef<T> works for any objects that are derived from wxTrackable. By default, wxEvtHandler and wxWindow derive from wxTrackable. However, wxObject does not, so types like wxFont and wxColour are not trackable. The example below shows how to create a wxObject derived class that is trackable:

class wxMyTrackableObject : public wxObject, public wxTrackable
{
// ... other members here
};
wxObject
This is the root class of many of the wxWidgets classes.
Definition: object.h:233
wxTrackable
Add-on base class for a trackable object.
Definition: tracker.h:30

The following types of weak references are predefined:

typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
typedef wxWeakRef<wxWindow> wxWindowRef;

Template Parameters

  T | The type to which the smart pointer points to.

##

Library: None; this class implementation is entirely header-based.
Category: Smart Pointers

See also
  wxSharedPtr<T>, wxScopedPtr<T>

## Public Types

typedef T | element_type
| Type of the element stored by this reference. More...

## Public Member Functions

| wxWeakRef (T *pobj=NULL)
| Constructor. More...
| wxWeakRef (const wxWeakRef< T > &wr)
| Copy constructor. More...
virtual | ~wxWeakRef ()
| Destructor. More...
virtual void | OnObjectDestroy ()
| Called when the tracked object is destroyed. More...
void | Release ()
| Release currently tracked object and rests object reference. More...
T * | get () const
| Returns pointer to the tracked object or NULL. More...
T * | operator= (wxWeakRef< T > &wr)
| Release currently tracked object and start tracking the same object as the wxWeakRef wr. More...
T * | operator* () const
| Implicit conversion to T*. More...
T & | operator* () const
| Returns a reference to the tracked object. More...
T * | operator-> ()
| Smart pointer member access. More...
T * | operator= (T *pobj)
| Releases the currently tracked object and starts tracking pobj. More...

## Member Typedef Documentation

## ◆ element_type

template<typename T >

typedef T wxWeakRef< T >::element_type

Type of the element stored by this reference.

## Constructor & Destructor Documentation

## ◆ wxWeakRef() [1/2]

template<typename T >

wxWeakRef< T >::wxWeakRef | ( | T * | pobj = `NULL` | ) |

Constructor.

The weak reference is initialized to pobj.

## ◆ wxWeakRef() [2/2]

template<typename T >

wxWeakRef< T >::wxWeakRef | ( | const wxWeakRef< T > & | wr | ) |

Copy constructor.

## ◆ ~wxWeakRef()

template<typename T >

virtual wxWeakRef< T >::~wxWeakRef | ( |  | ) |

virtual

Destructor.

## Member Function Documentation

## ◆ get()

template<typename T >

T* wxWeakRef< T >::get | ( |  | ) | const

Returns pointer to the tracked object or NULL.

## ◆ OnObjectDestroy()

template<typename T >

virtual void wxWeakRef< T >::OnObjectDestroy | ( |  | ) |

virtual

Called when the tracked object is destroyed.

Be default sets internal pointer to NULL. You need to call this method if you override it.

## ◆ operator*() [1/2]

template<typename T >

T* wxWeakRef< T >::operator* | ( |  | ) | const

Implicit conversion to T*.

Returns pointer to the tracked object or NULL.

## ◆ operator*() [2/2]

template<typename T >

T& wxWeakRef< T >::operator* | ( |  | ) | const

Returns a reference to the tracked object.

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

## ◆ operator->()

template<typename T >

T* wxWeakRef< T >::operator-> | ( |  | ) |

Smart pointer member access.

Returns a pointer to the tracked object. If the internal pointer is NULL this method will cause an assert in debug mode.

## ◆ operator=() [1/2]

template<typename T >

T* wxWeakRef< T >::operator= | ( | T * | pobj | ) |

Releases the currently tracked object and starts tracking pobj.

A weak reference may be reset by passing NULL as pobj.

## ◆ operator=() [2/2]

template<typename T >

T* wxWeakRef< T >::operator= | ( | wxWeakRef< T > & | wr | ) |

Release currently tracked object and start tracking the same object as the wxWeakRef wr.

## ◆ Release()

template<typename T >

void wxWeakRef< T >::Release | ( |  | ) |

Release currently tracked object and rests object reference.