方法,该方法接受类作为模板参数,并接受类构造函数的参数作为方法参数
method that takes a class as a template parameter, and takes the parameters of the classes constructor as method parameters
好了,我正在创建我自己的实体组件系统,我被困在AddComponent Entity方法,它将组件添加到实体中,下面是它的样子:
template <typename T>
void AddComponent() {
NumOfComponents++;
AllComponents.push_back(new T());
}
这个工作得很好,但如果我有一个组件构造函数呢?就像
class Transform : public Component
{
public:
Transfrm(Vector3f newPosition, Vector3f newRotation, Vector3f newScale) : Component("Transfrm") {};
Vector3f Position;
Vector3f Rotation;
Vector3f Scale;
~Transfrm();
};
我想要达到的是这样的:
Entity ent1;
Vector3f Pos, Rot, Scl;
ent1.AddComponent<Transform>(Pos, Rot, Scl); // This is currently not possible
我如何接受Transform的方法参数作为AddComponent方法参数,并实现类似于上面的东西?
这是参数包最简单的用例。
template <typename T, typename ...Args>
void AddComponent(Args && ...args) {
NumOfComponents++;
AllComponents.push_back(new T(std::forward<Args>(args)...));
}
要求至少c++ 11
相关文章:
- c++方法参数只能在linux的发布模式下自行更改
- 使用移动语义:右值引用作为方法参数
- 无法将动态创建的对象数组从 qml 发送到 c++ 作为方法参数
- 将方法参数类型更改为子类中的派生类
- ofstream作为C 中的方法参数
- 使用 gmock 匹配器将 std::function 设置为 EXPECT_CALL 中的方法参数
- C com 方法参数扣除
- 获取非静态方法参数计数
- 使用 gmock 返回模拟方法参数
- 运行 QML 时出现"未知方法参数类型"错误
- 基本C - 构造contaning对象引用,并将其作为方法参数传递
- 将C 方法参数转换为模板参数会因编译错误而失败
- 如何在方法参数中使用boost :: asio :: buffer
- qsharedPointer作为方法参数或方法的返回值的良好实践
- 如何将方法指针声明为Typedef方法参数
- 方法参数中的 OpenCV 垫"预期")""
- 方法参数中的 consst* 常量
- 带有“class”详细类型说明符的方法参数
- 具有原始方法参数派生类的 C++ 重载方法参数
- C++ std::string length() 或 size() 不适用于方法参数