【发布时间】:2022-01-02 09:25:03
【问题描述】:
标题可能有点混乱,但基本上我有一个“四元数”类,它有 2 个参数,第一个是 Vector3 类的另一个实例,另一个是浮点数。
Vector3 将 3 个浮点数作为参数并将它们分配给 x、y 和 z。
我想为 Quaternion 类设置默认参数,但我不确定如何以类为参数设置默认参数。
Vector3
class Vector3 {
public:
float x, y, z;
Vector3(float uX, float uY, float uZ) {
this->x = uX;
this->y = uY;
this->z = uZ;
}
};
四元数
class Quaternion {
public:
Vector3 axis;
float scalar;
Quaternion(Vector3 uAxis, float uScalar = 0) {
axis = uAxis;
scalar = uScalar;
};
};
我想将uAxis 的默认参数设为Vector3,其中x、y 和z 分别设置为1, 0, 0,但我不确定如何做到这一点。
【问题讨论】:
-
我喜欢将静态默认值添加到矢量类本身的模式。也许
up、right和forward。然后,您可以使用Vector3 uAxis = Vector3::right或您环境中的任何 x。但我相信Vector3 uAxis = Vector3(1.f, 0.f, 0.f)也会起作用。