解决方法覆盖模板?
我正在尝试构建实体组件系统。这是我拥有的:
一个可以具有组成部分的实体:这些实体源自基本的“组件”类,以携带诸如ongreated(),)等常见方法。 现在,我想添加可以在组件上操作以进行实际计算的“系统”。 例如,“物理”系统将在所有物理组件上循环以更新它们,“渲染器”系统将循环通过网格组件等
。类型。然后,任何想要由某些系统使用的组件只需要覆盖该系统的特定更新方法即可。这是一个想法:
// base component class
class Component {
template<class S>
virtual void Update(float _deltaTime) { } // empty on base component
}
// system class (inherits from the system base class)
class Physics : System {
void Update(float _deltaTime) {
// call Update<Physics>(float _deltaTime) on every concerned component
}
}
// component class somewhat related to the physics system
class Collider : Component {
void Update<Physics>(float _deltaTime) { /* calculations */ }
}
现在,我从这里覆盖模板方法。 当我仍在学习C ++时,我想不出一个可以允许我想要的系统。 那么,有什么更聪明 /可能的方法可以做我想实现的目标吗?
提前致谢 :
I'm trying to build a entity component system. Here is what I have :
An entity, that can have components : these are derived from a base "Component" class to carry common methods like OnCreated(), etc...
Now, I want to add "Systems" that can operate on components to do the actual calculations.
For example, a "Physics" system would loop over all physics component to update them, a "Renderer" system would loop over mesh components, etc...
My first idea was to have an template Update method, taking a system as a template type. Then, any component that want to be used by some system would only need to override that specific Update method for that system. Here was the idea :
// base component class
class Component {
template<class S>
virtual void Update(float _deltaTime) { } // empty on base component
}
// system class (inherits from the system base class)
class Physics : System {
void Update(float _deltaTime) {
// call Update<Physics>(float _deltaTime) on every concerned component
}
}
// component class somewhat related to the physics system
class Collider : Component {
void Update<Physics>(float _deltaTime) { /* calculations */ }
}
Now, I've read from here that it is not possible to override template methods.
As I'm still learning C++, I can't think of a system that would allow what I want.
So is there any smarter / possible ways to do what I want to achieve ?
Thanks in advance :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论