Entity
在World.h中前向声明类。
世界.h:
class Entity; // #include "Entity.h" is not needed, because
// only a pointer to Entity is used at the moment.
class World {
public:
void foo() {}
void letEntityDoFooToMe(); // Implementation must be defined later, because it
// will use the whole Entity class, not just a
// pointer to it.
private:
Entity* e;
};
实体.h:
#include "World.h" // Needed because Entity::doFooToWorld calls a method of World.
class Entity {
public:
Entity(World& world) : w(world) {}
void doFooToWorld() {
w.foo();
}
private:
World& w;
};
世界.cpp:
#include "World.h" // Needed because we define a method of World.
#include "Entity.h" // Needed because the method calls a method of Entity.
void World::letEntityDoFooToMe() {
e->doFooToWorld();
}