开发者

C++: Conceptual circular include problem

开发者 https://www.devze.com 2023-01-23 13:09 出处:网络
I\'m making a component based entity system for a game engine. I have an entity class, which has to include the component base class header in order to define the array of components private Componen

I'm making a component based entity system for a game engine.

I have an entity class, which has to include the component base class header in order to define the array of components private Component* components[ 123 ]

However, in the component base class I have to define a priv开发者_StackOverflowate Entity* ownerEntity, beacuse it is crucial that a component knows who it belongs to!

This results in Entity.h needing Component.h and vice-versa -> Circular reference

How can I solve this?


As long as you only need your class to contain pointers or references to the other classes, you can skip the real include file and use an empty forward declaration like class Component;.

You will need the full include in the source file where you dereference the pointers or use the reference to call methods on Component.

0

精彩评论

暂无评论...
验证码 换一张
取 消