C++: Conceptual circular include problem(C++:概念循环包含问题)
问题描述
我正在为游戏引擎制作基于组件的实体系统.
I'm making a component based entity system for a game engine.
我有一个实体类,它必须包含组件基类标头才能定义组件数组private Component* components[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 ]
然而,在组件基类中我必须定义一个private Entity* 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?
推荐答案
只要你的类只需要包含 pointers 或 references 到其他类,你可以跳过真正的包含文件并使用像 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;
.
您将需要在源文件中完整包含您取消引用指针或使用引用来调用 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
.
这篇关于C++:概念循环包含问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++:概念循环包含问题


- Qt计时器使用方法详解 2023-05-30
- C++ 数据结构超详细讲解顺序表 2023-03-25
- ubuntu下C/C++获取剩余内存 2023-09-18
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- Easyx实现扫雷游戏 2023-02-06
- C语言详解float类型在内存中的存储方式 2023-03-27
- C语言qsort()函数的使用方法详解 2023-04-26
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11