C ++:概念循环包括问题 [英] C++: Conceptual circular include problem

查看:95
本文介绍了C ++:概念循环包括问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏引擎制作一个基于组件的实体系统。

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

我有一个实体类,它必须包含组件基类头以便定义组件数组私有组件*组件[123]

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 ]

但是,在组件基类中我必须定义一个私有实体* ownerEntity ,因为一个组件知道它属于谁是至关重要的!

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

这个结果在Entity.h中需要Component.h,反之亦然 - >循环引用

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

我该如何解决这个问题?

How can I solve this?

推荐答案

只要你只需要你的类将指针引用包含到其他类中,你就可以跳过真正的包含文件并使用一个空的前向声明,如 class Component;

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;.

您需要在源文件中包含完整的包含在哪里取消引用指针或使用引用来调用 Compon上的方法ent

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

这篇关于C ++:概念循环包括问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆