Re: Deriving from interface implementation



Glib::ObjectBase is a virtual base class. The constructor of a virtual base class is called from the most derived class. When ModelImpl is the most derived class, its constructor calls Glib::ObjectBase(typeid(ModelImpl)). When Derived is the most derived class, Glib::ObjectBase(typeid(ModelImpl)) is not called. Instead ObjectBase's default constructor is called from Derived's constructor. That's not what you want in this case. You must call the right ObjectBase constructor from Derived's constructor, like

class Derived: public ModelImpl
{
    Derived()
    : Glib::ObjectBase(typeid(Derived)),
      ModelImpl() {}
};

Den 2021-08-04 kl. 12:15, skrev Sergey Smirnykh via gtkmm-list:
Hello!

I've run into the issue I can't resolve on my own. I have
a Gio::ListModel implementation and a (C++) class derived
from said implementation.

Calling G_IS_LIST_MODEL(gobj) on the implementation's gobj returns 1.
However, G_IS_LIST_MODEL(gobj) on the derived class's gobj returns 0.

How to make inheritance work? I've been unable to find any
guides/tutorials on this issue, hence the question.

The implementation is defined as follows:

    class Item { ... }

    class ModelImpl: public Gio::ListModel, public Glib::Object {
    public: ModelImpl(): Glib::ObjectBase(typeid(ModelImpl)),
        Gio::ListModel() {}
        virtual ~ModelImpl() = default;
    protected:
        std::vector<Item*> items;
        GType get_item_type_vfunc() override {
            return Item::get_type();
        }
        guint get_n_items_vfunc() override {
            return items.size();
        }
        gpointer get_item_vfunc(guint position) override {
            if (position < items.size()) {
                return items[position]->gobj();
            }
            return nullptr;
        }
    };

And the derived class:

    class Derived: public ModelImpl {
        Derived(): ModelImpl() {}
    }

Thanks in advance!

Sergey Smirnykh
_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]