Appendix A. The RefPtr smartpointer

## Appendix A. The RefPtr smartpointer

`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, and a future version of gtkmm might possibly use that instead.

Reference

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

## Copying

You can copy `RefPtr`s, 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);

Chapter 27. Contributing |  | Dereferencing