Re: Deriving from interface implementation

2021-08-05 Thread Sergey Smirnykh via gtkmm-list
Thank you!

On Wed, 4 Aug 2021 19:19:36 +0200
Kjell Ahlstedt  wrote:

> 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 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
> > [email protected]
> > https://mail.gnome.org/mailman/listinfo/gtkmm-list  

___
gtkmm-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/gtkmm-list


Re: Deriving from interface implementation

2021-08-04 Thread Kjell Ahlstedt via gtkmm-list
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 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
[email protected]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
___
gtkmm-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/gtkmm-list