Unexpected Behavior with Multiple Inheritance
Recently, I started using these macros to use Java-style interfaces in my C++ code. They allow me to gain the advantages of interfaces, without the madness of multiple inheritance.
As I was soon reminded, though, using anything in C++ without a solid understanding of what’s going on under the hood leads to certain doom. My event broadcasters were broadcasting, my event listeners were listening, but the event would come along and no one would do anything with it. What was going wrong?
- When can two pointers to the same object be unequal?
- When can calling a virtual function on an object call a superclass’s version, even if that object has overridden it?
- When can a base class see a different “this” pointer than its descendant sees?
When you’re using multiple inheritance! It all makes perfect sense once you know how multiple inheritance is implemented, but at first I found it unintuitive that pFoo and pBar could be unequal in the following code (more complete example here):
FooBar fb;
Foo* pFoo = &fb;
Bar* pBar = &fb;
If you’re hazy on that, too, you may want to check out this excellent resource. It talks about how multiple inheritance is handled by the compiler, and when to use dynamic_cast. I spent a few hours reading over it and writing test cases. Armed with my new knowledge, I replaced a couple select typecasts with dynamic_cast, and now my events are playing happily.

November 28th, 2006 at 5:08 am
This might be a testament to how lazy I am when it comes to the object-oriented stuff, but I have yet to stumble upon a case where I’ve had to use multiple inheritance. Heck, I’ve never used a vitual function or a static object, either, but I’m weird like that.
At the end of the day, I’ve managed to write a fair amount of code in perl or C, depending on how much I care about performance. What little C++ I write tends to be pretty straightforward, so much so that I wonder if multiple inheritance is a feature, or a kludge for not working out your class structure in a clean way to begin with. Either I’ve not seen the light, or I’ve not drank the kool-aid. Or I’m just being an obstinant jerk. Or something else.
As far as OOP goes, I’m still searching for an example that lives up to the claim of reusability that is oft-cited. OOP works well for me when I’m thinking about a problem in terms of data structures. Not so hot when I’m more concerned about the transformations on the data.
Seriously, what’s wrong with me? I haven’t been doing this nearly long enough to act like such a curmudgeon.
November 29th, 2006 at 9:26 pm
Oh, I agree– multiple inheritance is totally unjustified kludgery in most cases. In half the remaining cases, it’s somewhat justifiable, but still not worth the headaches.
Interfaces, OTOH, are friggin’ great. Event broadcasters and listeners are a great use case, though C# goes an extra step and pulls events out into their own jobby– which is pretty sweet, btw. (I <3 C#.)
In many cases, you’re better off just using a language that allows duck typing: http://en.wikipedia.org/wiki/Duck_typing. If I felt I could use Python for a large chunk of this game’s development, I probably would.