解决方法覆盖模板?

发布于 2025-02-09 02:28:23 字数 963 浏览 1 评论 0原文

我正在尝试构建实体组件系统。这是我拥有的:

一个可以具有组成部分的实体:这些实体源自基本的“组件”类,以携带诸如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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文