Appendix A. The RefPtr smartpointer

Table of Contents

Copying
Dereferencing
Casting
Checking for null
Constness


Google

Glib::RefPtr is a smartpointer. Specifically, it is a reference-counting smartpointer. You might be familiar with std::auto_ptr<>, which is also a smartpointer, but Glib::RefPtr<> is much simpler, and more useful. We expect a future version of the C++ Standard Library to contain a reference-counting shared smartpointer, so a future version of gtkmm will probably use that instead.

Reference

A smartpointer acts much like a normal pointer. Here are a few examples.

Copying

You can copy RefPtrs, just like normal pointers. But unlike normal pointers, you don't need to worry about deleting the underlying instance

Glib::RefPtr<Gdk::Bitmap> refBitmap = Gdk::Bitmap::create(window,
data, width, height);
Glib::RefPtr<Gdk::Bitmap> refBitmap2 = refBitmap;

Of course this means that you can store RefPtrs in standard containers, such as std::vector or std::list.

std::list< Glib::RefPtr<Gdk::Pixmap> > listPixmaps;
Glib::RefPtr<Gdk::Pixmap> refPixmap = Gdk::Pixmap::create(window,
width, height, depth);
listPixmaps.push_back(refPixmap);