I have a question concerning multiple inheritance. In my project, I want to add support for dll plugins and need an interface for that reason. In the application, I create objects like players and visual objects and hand them to the dll interface. the dll only has the ability to interpret a character pointer as ICharacter for example. My problem occurs, within the application itself:
class IGameObject
{
virtual const std::string &getName(void) const = 0;
virtual void setName(const std::string &name) = 0;
};
class IVob : public virtual IGameObject
{
virtual const Math::Vec3 &getPosition(void) const = 0;
virtual void setPosition(const Math::Vec3 &pos) = 0;
// more pure virtual functions
};
class ICharacter : public virtual IVob
{
// pure virtual functions
};
class GameObject : public virtual IGameObject
{
GameObject();
virtual const std::string &getName(void) const override;
virtual void setName(const std::string &name) override;
// more virtual function override
};
class Vob : public virtual IVob, public GameObject
{
Vob(VobType type);
virtual const Math::Vec3 &getPosition(void) const override;
virtual void setPosition(const Math::Vec3 &pos) override;
// more virtual function override
};
class Character : public ICharacter, public Vob
{
Character();
// virtual function override
};
Suppose character is of ICharacter
Now, if I want to call character->setPosition(pos);
it may crash, since it comes as ICharacter pointer. If I reinterpret_cast<Character*>(character)->setPosition(pos);
it, then it may work properly. It seems, as if the vtables got mixed up, but I can't see where that happens. If I reorder the inheritance it may – in some constellation – work.
Process looks like:
Creation (Character*
) -> dll interface (reinterpret_cast<ICharacter*>
) -> call functions in dll interface, which are 100% part of the original character
If passed back to the application:
dll interface (ICharacter*
) -> application (reinterpret_cast<Character *>
)
What do I miss?
Thanks in advance
Edit: In fact did I try to dynamic_cast
, but that didn't help. Now it crashes in an even earlier call.