Inheritance doubt

May 7, 2010 at 7:00pm
Hi there,

I need to build a class that the user should be able to use as a Gtk::Widget (basically to put it inside of a window). Let's say that for building my widget I'll need it to be a Gtk::VBox, which also inherits from Gtk::Widget.

Now, I could inherit my new class directly from Gtk::VBox, the user would then be able to use it just like a Gtk::Widget, and I would be able to make my implementation without a lot of trouble. The problem with this is that the user would be able to use public members of Gtk::VBox, but I don't want him to!
1
2
3
4
class MyClass : public Gtk::VBox {
...
}; // Oh my! The end-user could use MyClass like a Gtk::VBox!


My ideal solution would be:
- MyClass inherits from Gtk::Widget (public) : the end-user can therefore use MyClass just like a Gtk::Widget.
- MyClass ingerits from Gtk::VBox (private) : the end-user cannot use methods from Gtk::VBox.
- For a MyClass object there have to be one and only one instance of Gtk::Widget

So, reminding something about virtual classes, the basic idea would have been:
1
2
3
4
class MyClass : public virtual Gtk::Widget, private Gtk::VBox {
...
};


Another concern that blows my mind is about the implementation of Gtk::VBox : it does inherit from Gtk::Widget in a public fashion (i.e. not virtually). As a consequence I don't think the solution I come up with would work at all.

What do you think about?

EDIT: I forgot something: the Gtk::Widget object the user is referring to when using an instance of MyClass has to be the same which Gtk::VBox inherits from! (that's why I've put virtual there)
Last edited on May 8, 2010 at 5:37am
May 7, 2010 at 7:54pm
Why do you want to hide the VBox interface?
What should your widget do?
May 8, 2010 at 5:35am
Why do you want to hide the VBox interface?


I don't want the user to be able to call methods of Gtk::VBox, like Gtk::VBox::insert() and so on. I'd like my widget to be a ready-to-use and unmodifiable one.

What should your widget do?


Well actually it's a status box for a usb device, but this is not the point.


My real concern is about the language: is it possible do do such a thing?
May 8, 2010 at 9:22am
I mean, what should it do with the stuff inherited from the VBox?
private / protected inheritance can be avoided if you use a VBox member
May 8, 2010 at 12:04pm
Well, Gtk::VBox is a vertical box which acts as a container for widgets, basically I'll instantiate some widgets (members of MyClass) and add them to this one.

I don't think it is possible for Gtk::VBox to be a member in this case: the user has to be able of referring to a MyClass object as if he was referring to a Gtk::Widget (the same Gtk::Widget from which Gtk::VBox inherits), there must be some hierarchical relationship, mustn't it?
Topic archived. No new replies allowed.