Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to downcast RefPtr

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.

like image 397
Klaus Avatar asked Feb 12 '23 03:02

Klaus


2 Answers

For downcasting use

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

documentation

like image 150
Nikita Avatar answered Feb 15 '23 11:02

Nikita


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

like image 36
eerorika Avatar answered Feb 15 '23 12:02

eerorika