指向类模板方法的空指针的向量

PSquall

我正在开发一个 DataManager,可以在其中注册组件,它会创建一个Buffer<Component>. 这是有效的,但为了保存缓冲区,我有一个std::vector<char*>,因为我无法在同一个向量中保存不同类型的类模板。

        //register Components and return reference to them (Pre-Init Phase)
    template<class TComponent>
    ComponentID registerComponent(std::string Name = "",int ComponentNr = COMPONENTNR) {

        //Create Buffer<TComponent> with rising ID Counter
        ComponentContainer<TComponent>* t = new ComponentContainer<TComponent>(_componentIDCounter++, ComponentNr);

        //Add Buffer ptr to vector
        _container.push_back(reinterpret_cast<char*>(t));

        //return ComponentID
        return t->getComponentID();
    };

现在我希望 DataManager 成为一个用于创建和删除组件的大包装类。为此,我想保存一个指向新创建的缓冲区的 createComponent 方法的指针。问题是,我不能为给定的方法定义一个向量......

typedef void (*CreatesPtr)(int);
...
CreatesPtr f = t->createComponent;

...这不起作用,因为它认为我想创建一个指向成员的指针,而不是一个方法,或者我可以使用std::bind来绑定它,因为 std::bind,但是我不能定义一个向量对于我想调用每个类模板的函数。

也许我的方式只是简单的愚蠢,而且以其他方式实施起来要容易得多,所以任何建议都值得赞赏,但如果有办法使这项工作成功,我肯定会更喜欢那个。

提前致谢!

MSalters

您从一个未说明且不正确的假设开始。

类模板可以从非模板基类派生。您可以在向量中存储指向此基类的指针。

基类可以有纯virtual方法,您可以调用向量的元素。类模板将实现这些virtual方法,可能是根据其模板参数。

在您的情况下,它看起来像是createComponent虚拟方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章