6

I'm making a game GUI API where each widget inherits from the Widget class. I was thinking, when others make there own widgets, they might not be fully satisfied with the base class. They might want to add getTheme() for example. Would it be a good idea to make all my widgets virtually inherit from Widget then so that this is possible?

Thanks

jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • What do you mean by "virtual inheritance"? –  Apr 18 '11 at 14:50
  • 7
    @unapersson: http://en.wikipedia.org/wiki/Virtual_inheritance – Mat Apr 18 '11 at 14:51
  • @Mat I'm well aware of virtual base classes. However, C++ has no concept of virtual inheritance, or if it does it is not mentioned in the C++ standard. –  Apr 18 '11 at 14:57
  • @unapersson: yes it does. Look in section 10 "Derived classes" (i have that in draft n3092 of C++0x, but it's been there way before) – Mat Apr 18 '11 at 15:01
  • @Mat Nope, just been through that entire section - no use of the term "virtual inheritance". –  Apr 18 '11 at 15:23
  • @unapersson: how do you call inheritance that uses `: public virtual Base`? – Mat Apr 18 '11 at 15:25
  • @Mat The issue is not what I call it (I would call it derivation from a virtual base), but what the C++ Standard calls, or in this case doesn't call, it. –  Apr 18 '11 at 15:28
  • @unapersson: fair enough, call it whatever you want, I'll stick with how [Bjarn Stroustrup calls it in The C++ Programming Language, Third Edition](http://cpp-full.info/eBooks_.eBook..Addison.Wesley-The.C...Programming.Language.3rd.Ed..Stroustrup.ISBN.0201889544/pg_0409.htm) – Mat Apr 18 '11 at 15:34

4 Answers4

4

To resolve a diamond-shaped inheritance problem. (B and C both inherit from A. What happens to A's attributes in D that itself inherits from B and C?)

A client of your library could see a RedWidget and a FlyingWidget, and might want to combine them into a RedFlyingWidget.

User would have to specify one of the base classes to be virtual when inheriting. But that is not responsibility of a library maker.

OOP flows better with single-implementation inheritance, so that's what I'd use throughout a library.

There are also "upside-down inheritance" trees, as described by Alexandrescu's excellent "Modern C++ Design." They allow clients to pull in more functionality in a form of mix-ins that are called policies.

Programming with policies allows for greater ability to combine functionality, at the expense of syntactical cleanliness. Think STL implementation, for example.

GregC
  • 7,737
  • 2
  • 53
  • 67
  • 3
    it's not the methods you should be concerned about, but the data. If `A` is a pure interface, there is no issue. – Matthieu M. Apr 18 '11 at 15:01
  • Resolving is _what_ it is. The question is whether it's a good idea. – H H Apr 18 '11 at 15:08
  • @Milo: here's a related discussion: http://stackoverflow.com/questions/4605556/when-virtual-inheritance-is-a-good-design – GregC Apr 18 '11 at 15:56
4

Just because the user would add their own methods to a child class doesn't mean you need to use virtual inheritance. You would use it if, in your library, you have a single base class with multiple children, and people could inherit from multiple child classes at once (for example mixin rather than substitution).

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

When is virtual inheritance a good idea?

That's a design question.

For your Widgets, I would say Yes, multi-derived classes should have the option to be just 1 Widget.

H H
  • 263,252
  • 30
  • 330
  • 514
0

Whenever there is a possibility that the users of your library are going to use several classes from your library as a base class (ie derive from them), you have to use virtual inheritance. In other words, it is a good idea to use it in your case.

BЈовић
  • 62,405
  • 41
  • 173
  • 273