Using Multiple Inheritance to Implement Entity-Component Programming

I read several articles on the Entity-Component programming style. One common problem is how to express dependencies between components and how the components associated with the same object interact.

It seems to me that a simple solution to this problem is to make each dependency a virtual base class dependent on it.

Thus, when a component is included in an object (via virtual inheritance), all dependent components are included exactly once. In addition, all the functions that the component depends on will be available in its member functions.

class C_RigidBody : public virtual C_Transform {
    public void tick(float dt);
};

class C_Explodes : public virtual C_Transform {
    public void explode();
};

class E_Grenade : public virtual C_RigidBody, public virtual C_Explodes {
    //no members
};

Is there a reason no one does this?

( , , , - " ", - , . (, ))

+4
2

.

, , . .

, DIP :

  • . .

  • . .

.

struct IClockService
{
    virtual unsigned timestamp() = 0;
};

struct ITimingService
{
    virtual unsigned timing(std::function<void()> f) = 0;
};

struct ClockService : public virtual IClockService // public virtual means implement
{
    virtual unsigned timestamp() { return std::time(nullptr); }
};

struct TimingService : public virtual ITimingService
                     , private virtual IClockService // private virtual means dependency
{
    virtual unsigned timing(std::function<void()> f)
    {
        auto begin = timestamp();
        f();
        return timestamp() - begin;
    }
};

class Application : public ClockService
                  , public TimingService
{
};
Application a; 
unsigned runingTime = a.timing(anyFunction);

. ClockService TiminigService - , , IClockService ITimingService. Application .

:

  • , Liskov-Subtitution
  • .
  • , , DIP.
  • , - , , .

. .

+1

, , .

ECS . , , "is-a" . , , , , , , , "".

"" . , . , , , , .

, , , , , "is-a" (: , , dtor , , , , , ).

, , . , , , . , , , . , , , , . , , .

Runtime

, , , , .. . , , (, Lua) , .

. , , ( ).

, , , , , . , .

, RTTI , , , ABI, vptr .., , . .

0

Source: https://habr.com/ru/post/1526447/


All Articles