glibmm  2.74.0
Typedefs
RefPtr

A reference-counting shared smartpointer. More...

Typedefs

template<class T_CppObject >
using Glib::RefPtr = std::shared_ptr< T_CppObject >
 RefPtr<> is a reference-counting shared smartpointer. More...
 

Detailed Description

A reference-counting shared smartpointer.

Typedef Documentation

◆ RefPtr

template <class T_CppObject >
using Glib::RefPtr = typedef std::shared_ptr<T_CppObject>

RefPtr<> is a reference-counting shared smartpointer.

Some objects in gtkmm are obtained from a shared store. Consequently you cannot instantiate them yourself. Instead they return a RefPtr which behaves much like an ordinary pointer in that members can be reached with the usual object_ptr->member notation.

Reference counting means that a shared reference count is incremented each time a RefPtr is copied, and decremented each time a RefPtr is destroyed, for instance when it leaves its scope. When the reference count reaches zero, the contained object is deleted, meaning you don't need to remember to delete the object.

RefPtr is a std::shared_ptr with a special deleter. To cast a RefPtr<SomeType> to a RefPtr<SomeOtherType>, use one of the standard library functions that apply a cast to the stored pointer, for instance std::dynamic_pointer_cast.

Example:

Glib::RefPtr<const Gio::ListModel> monitors = Gdk::Display::get_default()->get_monitors();
Glib::RefPtr<const Glib::ObjectBase> first_object = monitors->get_object(0);
std::dynamic_pointer_cast<const Gdk::Monitor>(first_object);

See the "Memory Management" section in the "Programming with gtkmm" book for further information.

See also
Glib::make_refptr_for_instance() if you need to implement a create() method for a Glib::ObjectBase-derived class.