使C++模拟类同时使用 2D 和 3D 矢量
Making a C++ Simulation-Class work both with 2D- and 3D-Vectors
d我正在编程一个模拟。现在它应该同时适用于 2D 和 3D,所以我正在尝试让我的类使用 2D 和 3D 矢量。此外,矢量应具有模板参数,以指示应将哪种类型用于坐标。
我的基类如下所示:
class SimulationObject {
AbstractVector<int>* position;
AbstractVector<float>* direction;
}
现在的问题是,我不能使用多态性,因为那时我所有的 Vector 都必须是指针,这使得运算符重载几乎不可能用于这样的操作:
AbstractVector<float>* difference = A.position - (B.position + B.direction) + A.direction;
但我也不能使用模板参数来指定要使用的类型:
template <typename T> class Vector2d;
template <typename T> class Vector3d;
template <class VectorType> class SimulationObject {
VectorType<int> position;
VectorType<float> direction;
}
SimulationObject<Vector2D> bla;
//fails, expects SimulationObject< Vector2D<int> > for example.
//But I don't want to allow to specify
//the numbertype from outside of the SimulationObject class
那么,怎么办呢?
您可以使用模板模板参数:
template <template <class> class VectorType>
class SimulationObject {
VectorType<int> position;
VectorType<float> direction;
};
相关文章:
- 3D 矢量在球体上迭代
- 尝试访问 3D 矢量中的元素时出现 Seg 错误
- 计算每个维度的两个 3D 矢量之间的角度
- 如何使用偏航、俯仰和滚动旋转 3D 矢量?
- C++ 和 Lua 函数之间的交互与 3D 矢量参数
- 用于 3D 矢量的高效除法运算符
- 将 3D 矢量浮点打包到无符号 int 中并将其解压缩
- 3D 矢量 - 变换和减号
- 我如何将一组整数集变成C 中的3D矢量
- 3D 矢量 - "No instance of overload function?"
- 在转换3D矢量时,为什么GLM给出错误的结果
- 如何获取3D矢量的剩余轴知道其他两个
- 提升program_options:在 3D 矢量中读取作为命令行参数
- 特征:从 3D 矢量 c++ 中减去标量
- 如何重新计算启动3D矢量的构造函数
- 以C++格式将数据从文本文件读取到 3D 矢量中
- 算术在3D矢量中获取索引
- C++ 最快的 3D 矢量循环扫描
- 尝试创建一个 3D 矢量,该矢量将保留指向指向无符号字符的指针的指针
- 3d矢量算术模板(如何专门用于int和float版本)