How to disable RefPtr

In Gtk, I have a class hierarchy:

Gtk :: ListStore derives from Gtk :: TreeModel

From Gtk::TreeView

I can get get_model()

aGlib::RefPtr<Gtk::TreeModel>

If I use in my Gtk::TreeView

Gtk :: ListStore as Gtk :: TreeModel and call get_model()

, I getGlib::RefPtr< TreeModel >

But I need call member functions from Gtk :: ListStore.

How do I do Glib::RefPtr<Gtk::TreeModel>

before Glib::RefPtr<Gtk::ListStore>

. Is there a standard way or is there a need for a hack or how does the typical procedure work with views and different types of storage. Are there any validation functions so that upcast can do safe, perhaps with a compile-time check, or if this is not possible at runtime? Any hint of any documentation.

+3


source to share


2 answers


To use downcasting

Glib::RefPtr<Gtk::ListStore> ptrDerived = Glib::RefPtr<Gtk::ListStore>::cast_dynamic(treeModel);

      



documentation

+5


source


The attachment is implicit, as shown in the documentation linked by nikitoz.



However, Gtk::ListStore

it is not a base class Gtk::TreeModel

, but vice versa. You are asking for a downgrade, not a rise. You need an explicit dynamic cast for this. Glib::RefPtr

has a static member function template cast_dynamic

that does exactly that. You can find it in the class description

+2


source







All Articles