首页 > 解决方案 > C ++ - 从向量中删除特定的智能指针

问题描述

我目前正在研究一个实体组件系统。每个实体都有一个unique_ptrs 到组件的向量(我有一个基类调用Component,每个组件都从它继承。还有一个std::bitset和一个std::array组件。要添加一个组件,我这样做:

template <typename T, typename... TArgs>
void add(TArgs&&... mArgs) {
    if (!componentBitset[getComponentTypeID<T>()]) { // make sure entity doesn't already have this component

        T* c(new T(std::forward<TArgs>(mArgs)...)); // create component
        c->object = this; // set the component's pointer to its parent entity
        std::unique_ptr<Component>uPtr{ c };
        components.emplace_back(std::move(uPtr));

        componentArray[getComponentTypeID<T>()] = c; // add to the std::array
        componentBitset[getComponentTypeID<T>()] = true; // set the bitset

        c->init(); // initialise the new component
    }
    else {
        // ...Object already has the component
    }
}

这被调用: entity->add<*component type*>(*constructor arguments*);

我的问题是:如何使用模板删除组件?不需要从 中删除std::array,因为如果它在位集中不存在,它将被覆盖。我知道如何设置位集,我只需要知道如何从向量中删除组件。

template <typename T> void remove() {
    // remove from vector here

    componentBitset[getComponentTypeID<T>()] = false;
}

标签: c++

解决方案


推荐阅读