4

In Gtk I have a class hierarchy like this:

Gtk::ListStore is derived from Gtk::TreeModel

From a Gtk::TreeView I can get with get_model() a Glib::RefPtr<Gtk::TreeModel>

If I use in my Gtk::Treeview a Gtk::ListStore as a Gtk::TreeModel and call get_model()I get a Glib::RefPtr< TreeModel >

But I want call member functions of Gtk::ListStore.

How can I cast the Glib::RefPtr<Gtk::TreeModel> down to Glib::RefPtr<Gtk::ListStore>. Is there a standard way or is there a hack needed or how is the typical procedure to act with views and different store types. Are there any checking functions so that the upcast can done safe, maybe with compile time checking or if not possible during runtime? Any hint to any documentation.

Klaus
  • 24,205
  • 7
  • 58
  • 113

2 Answers2

6

For downcasting use

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

documentation

Nikita
  • 950
  • 8
  • 19
2

Upcasting is implicit as shown in the documentation linked by nikitoz.

However, Gtk::ListStore is not a base class of Gtk::TreeModel but other way round. You are asking about downcasting, not upcasting. For that, you need explicit dynamic cast. Glib::RefPtr has a static member function template cast_dynamic which does exactly that. You can find it in the class reference

eerorika
  • 232,697
  • 12
  • 197
  • 326