0

I have trouble to convert a Glib::RefPtr into a GtkWidget, with T derived from Widget:

#include <gtkmm/drawingarea.h>
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/fixed.h>

class MyPic : public Gtk::DrawingArea {
public:
};

int main(int argc, char* argv[]) {
    Gtk::Fixed f;

    Gtk::DrawingArea da; // this works.
    Gtk::DrawingArea mp; // this works.
    Glib::RefPtr<MyPic> rp_mp; // this not.

    f.put(da, 10, 20);
    f.put(mp, 10, 30);
    f.put(rp_mp, 10, 40); // Line # 19
}   

This does not compile:

joerg> g++ x.cpp `pkg-config --cflags --libs gtkmm-3.0`
x.cpp: In function ‘int main(int, char**)’:
x.cpp:19:24: error: no matching function for call to ‘Gtk::Fixed::put(Glib::RefPtr<MyPic>&, int, int)’
x.cpp:19:24: note: candidate is:
/usr/include/gtkmm-3.0/gtkmm/fixed.h:123:8: note: void Gtk::Fixed::put(Gtk::Widget&, int, int)
/usr/include/gtkmm-3.0/gtkmm/fixed.h:123:8: note:   no known conversion for argument 1 from ‘Glib::RefPtr<MyPic>’ to ‘Gtk::Widget&’

joerg> g++ --version

g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Glib::RefPtr is a smart pointer, and DrawingArea is derived from Widget, so this should work.

Dereferencing (like f.put(*rp_mp,...) ) is intentionally not working. The documentation states: "*Unlike most other smart pointers, RefPtr doesn't support dereferencing through * object_ptr.*"

How do I get a Widget& from the SmartPtr?

Jörg Beyer
  • 3,631
  • 21
  • 35
  • What about dereferencing it?? – πάντα ῥεῖ Sep 27 '12 at 13:43
  • dereferencing (like f.put(*rp_mp,10,40) ) will not work and is documented to not work "Unlike most other smart pointers, RefPtr doesn't support dereferencing through * object_ptr.", from http://developer.gnome.org/glibmm/unstable/classGlib_1_1RefPtr.html. I added it to the Question. – Jörg Beyer Sep 27 '12 at 13:52
  • That's a really odd behavior for s.th. called a 'smart pointer', but if it's like this, it is. – πάντα ῥεῖ Sep 27 '12 at 16:15
  • 3
    The question is why you have a `RefPtr` of a widget in the first place. `RefPtr` is designed for pooled resources, not widgets. – ecatmur Sep 27 '12 at 17:20
  • This is a striped down example, the real code is longer. I used this RefPtr, because I collect the Widgets in a `std::vector`. It's a Gtk::Fixed with a flexible number of Thumbnail Pictures, realized as a Derived Widget (from DrawingArea). So, if you could suggest a better way to put a variing number of my widgets in a Container, that would help also. – Jörg Beyer Sep 27 '12 at 18:22
  • @JörgBeyer The maintainer of gtkmm is on record as stating that `Glib::RefPtr` is not meant as a smart pointer, or any case except when the API forces you to use it: http://stackoverflow.com/a/29156111/2757035 – underscore_d Jul 06 '16 at 19:55

1 Answers1

1

Although I suspect you can design around the need, you can dereference e.g. Glib::RefPtr foo as follows:

some_method_needing_a_reference(*foo.operator->())
ergosys
  • 47,835
  • 5
  • 49
  • 70