Recursos compartidos

Algunos objetos, como Gdk::Pixbuf y Pango::Font, se obtienen de un almacén compartido. Por lo tanto, no puede instanciar sus propias instancias. Estas clases típicamente heredan de Glib::Object. En lugar de requerirle referenciar y desreferenciar estos objetos, gtkmm usa el puntero inteligente Glib::RefPtr<>. Cairomm tiene su propio puntero inteligente, Cairo::RefPtr<>.

Objects such as Gdk::Pixbuf can only be instantiated with a create() function. For instance,

auto pixbuf = Gdk::Pixbuf::create_from_file(filename);

You have no way of getting a bare Gdk::Pixbuf. In the example, pixbuf is a smart pointer, so you can do this, much like a normal pointer:

auto width = 0;
if(pixbuf)
{
  width = pixbuf->get_width();
}

Cuando pixbuf salga del alcance, un unref() sucederá en segundo plano y ya no necesitará preocuparse más de él. No hay new, por lo que no hay delete.

If you copy a RefPtr, for instance

auto pixbuf2 = pixbuf;
, or if you pass it as a method argument or a return type, then RefPtr will do any necessary referencing to ensure that the instance will not be destroyed until the last RefPtr has gone out of scope.

Consulte el apéndice para obtener información detallada acerca de «RefPtr».

If you wish to learn more about smartpointers, you might look in these books:

  • Bjarne Stroustrup, "The C++ Programming Language" Forth Edition - section 34.3
  • Nicolai M. Josuttis, "The C++ Standard Library" - section 4.2