I
Imre
Hi!
I've got some questions regarding heavy use of virtual inheritance.
First, let's see a theoretical situation, where I might feel tempted to
use a lot of virtual inheritance.
Let's suppose, we're creating a little strategy game. In our game,
there are Units. A Unit can be either a Human, or a Vehicle. Obviously,
Human and Vehicle are subclasses of Unit.
Now let's suppose that we'd like to use protocol classes, to separate
interface from implementation as much as possible. So we create a
UnitIntf class, and a UnitImpl derived from it. Likewise, we have
HumanIntf and HumanImpl. HumanIntf, besides inheriting from HumanIntf,
should also inherit from UnitImpl. Our class hierarchy now looks like
this (let's forget about Vehicles for now):
UnitIntf
/ \
UnitImpl HumanIntf
\ /
HumanImpl
Furthermore, let's suppose, that we want to seprate functionality
common to most strategy games from functionality specific to our
current game, so that we may easily reuse the common parts in our next
game (or in another one in parallel development). That is, we want to
create a GameEngine layer, and on top of that, a Game layer. So, in our
Engine, we have Unit and Human, interfaces and implementations. In
Game, we have to introduce some Game-specific stuff on the Unit level,
and then some other game-specific things on the Human level. So we will
need GameUnit and GameHuman classes. If we still stick to the interface
/ implementation separation, then we'll have the following classes and
inheritances:
EngineUnitIntf
EngineUnitImpl: EngineUnitIntf
EngineHumanIntf: EngineUnitIntf
EngineHumanImpl: EngineHumanIntf, EngineUnitImpl
GameUnitIntf: EngineUnitIntf
GameUnitImpl: GameUnitIntf, EngineUnitImpl
GameHumanIntf: GameUnitIntf, EngineHumanImpl
GameHumanImpl: GameHumanIntf, GameUnitImpl, EngineHumanImpl
I won't even try to draw this. (Actually, it would be easy in 3d: it's
a nice cube. On one axis, there's Engine / Game, on the other Unit /
Human, and the third Intf / Impl.)
Such design would make heavy use of virtual inheritance. In fact, _all_
the inheritances should be virtual (at least, if we suppose that Human
may have further subclasses).
Although the hierarchy may seem complicated at first, I think it can be
get used to, and once one's used to it, it can even be convenient to
use.
However, I'm not sure how much run-time overhead this would cause.
How is virtual inheritance typically implemented on current compilers?
Am I overusing inheritance here? What design alternatives are there to
create something similar? What's the performance comparison between
protocol classes vs. the pimpl idiom?
Thanks,
Imre
I've got some questions regarding heavy use of virtual inheritance.
First, let's see a theoretical situation, where I might feel tempted to
use a lot of virtual inheritance.
Let's suppose, we're creating a little strategy game. In our game,
there are Units. A Unit can be either a Human, or a Vehicle. Obviously,
Human and Vehicle are subclasses of Unit.
Now let's suppose that we'd like to use protocol classes, to separate
interface from implementation as much as possible. So we create a
UnitIntf class, and a UnitImpl derived from it. Likewise, we have
HumanIntf and HumanImpl. HumanIntf, besides inheriting from HumanIntf,
should also inherit from UnitImpl. Our class hierarchy now looks like
this (let's forget about Vehicles for now):
UnitIntf
/ \
UnitImpl HumanIntf
\ /
HumanImpl
Furthermore, let's suppose, that we want to seprate functionality
common to most strategy games from functionality specific to our
current game, so that we may easily reuse the common parts in our next
game (or in another one in parallel development). That is, we want to
create a GameEngine layer, and on top of that, a Game layer. So, in our
Engine, we have Unit and Human, interfaces and implementations. In
Game, we have to introduce some Game-specific stuff on the Unit level,
and then some other game-specific things on the Human level. So we will
need GameUnit and GameHuman classes. If we still stick to the interface
/ implementation separation, then we'll have the following classes and
inheritances:
EngineUnitIntf
EngineUnitImpl: EngineUnitIntf
EngineHumanIntf: EngineUnitIntf
EngineHumanImpl: EngineHumanIntf, EngineUnitImpl
GameUnitIntf: EngineUnitIntf
GameUnitImpl: GameUnitIntf, EngineUnitImpl
GameHumanIntf: GameUnitIntf, EngineHumanImpl
GameHumanImpl: GameHumanIntf, GameUnitImpl, EngineHumanImpl
I won't even try to draw this. (Actually, it would be easy in 3d: it's
a nice cube. On one axis, there's Engine / Game, on the other Unit /
Human, and the third Intf / Impl.)
Such design would make heavy use of virtual inheritance. In fact, _all_
the inheritances should be virtual (at least, if we suppose that Human
may have further subclasses).
Although the hierarchy may seem complicated at first, I think it can be
get used to, and once one's used to it, it can even be convenient to
use.
However, I'm not sure how much run-time overhead this would cause.
How is virtual inheritance typically implemented on current compilers?
Am I overusing inheritance here? What design alternatives are there to
create something similar? What's the performance comparison between
protocol classes vs. the pimpl idiom?
Thanks,
Imre