创建不同模板对象的模板数组的模板数组

时间:2019-09-26 18:17:29

标签: c++ templates arrayofarrays

嘿,所以我目前正在一个非常简单的ecs上工作,我想将我所有的Components存储在数组中,这些数组有不同的类型,也有不同的类型,并且所有这些数组在一个更大的阵列中。 像这样:

ComponentArray< ComponentArray< Component > > components;

现在我了解了这一点,显然我必须为ComponentArrayComponent创建一个基类,并让MainArray拥有指向该基类的指针。 所以我认为声明应该像这样:

ComponentArray< ComponentArrayBase< ComponentBase* >* > components;

在另一个实现文件中的初始化中(因为它是一个静态类,并且具有模板成员函数,所以我必须将实现设为.inl文件,并且不能在那里初始化数组):

ComponentArray< ComponentArrayBase< ComponentBase* >* > ComponentManager::components{/*creating some arrays here*/};

我的Array类如下:

template<typename ComponentType>
struct ComponentArrayBase{};

template<typename ComponentType>
struct ComponentArray : ComponentArrayBase<ComponentType>
{
    ComponentType array[maxEntitys];

    ComponentType& operator[](unsigned int index)
    {
        return array[index];
    }
};

我的Components看起来像这样:

struct ComponentBase{};

struct PositionComponent : ComponentBase
{
    //some member variables
};

我要在哪里使用它,例如:

template<typename ComponentType>
ComponentType& ComponentManager::getComponent(Entity e, unsigned char index)
{
    return *(*(components[index])[e]);
}

编译时出现以下错误:

error: no match for ‘operator*’ (operand type is ‘ComponentArrayBase<ComponentBase*>’)
     return *(*(components[componentArray])[e]);

我不确定我所说的话是否有意义,非常感谢您的回答

Che

0 个答案:

没有答案
相关问题