c++ - C ++ - 从向量中删除特定的智能指针
问题描述
我目前正在研究一个实体组件系统。每个实体都有一个unique_ptr
s 到组件的向量(我有一个基类调用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;
}
解决方案
推荐阅读
- rstudio - 计算搜索词 - rstudio
- c# - 如何搜索列表
与用户输入字符串? - python - 使用 tf.py_func 作为损失函数实现梯度下降
- html - 我无法按下按钮,当我将它们居中时选择框(文本对齐:居中;)
- react-native - 如何在点击反应导航之外的按钮时跳转到根目录
- admin-on-rest - 反应管理员 | 无法使用 FileInput 上传文件
- liquibase - 我的 databaseChangeLog 层次结构有意义吗?
- linux - 如何监控繁忙线程的健康状况?
- liferay - 如何在 Asset Publisher 中嵌入 portlet?
- django - 如何使 django-rules 在模板中工作?