Download

Inheritance issue

Hi there,

I have two classes, one derived from APawn, and other derived from ACharacter. The first one is for the guard camera and the second one is for the bot that can move around. These two are mechanic and have similar properties and functionality, so I want to implement it in a generic parent class, say ABotMechanic. The problem is that I can’t inherit two UObject classes, in other words



class ABotMechanic: public APawn
{
...
};

class ABotCamera: public ABotMechanic
{
...
};

class ABotPatrol: public ACharacter, public ABotMechanic
{
..
};


won’t compile, because ABotPatrol tries to inherit two UObject subclasses.

Any ideas how to work around this problem?

Perhaps, deriving ABotMechanic from ACharacter, but is it good? For ABotCamera I don’t need a movement component and other move-around stuff.

Congratulations! You have discovered the deadly diamond of death!

C++ is nice in that it is one of the few languages which still allows multiple inheritance. A lot of other languages only let you inherit one class and anything else needs to be done with interfaces. Still, that makes it really easy to shoot yourself in the foot with things like the diamond of death.

You might want to look at object composition instead of object inheritance here, or look at some of the mitigation solutions offered up on the wikipedia article.

Nice! =D I suspected it is a known issue, because there wasn’t an evident solution how to resolve virtual methods. Using object composition is a good idea in my situation. I could incapsulate some logic in a component, e.g. UBotSensorComponent, which represents bot’s eye and ear.

I love UE4 community!