如何在C++中声明静态常量值? 我想能够得到常量Vector3 :: Xaxis,但我不应该能够改变它。C++类中的静态常量成员
我已经看到了另一大类下面的代码:
const MyClass MyClass::Constant(1.0);
我试图执行,在我的课:
static const Vector3 Xaxis(1.0, 0.0, 0.0);
但是我得到的错误
math3d.cpp:15: error: expected identifier before numeric constant
math3d.cpp:15: error: expected ‘,’ or ‘...’ before numeric constant
然后我尝试了一些更类似于我在C#中所做的事情:
static Vector3 Xaxis = Vector3(1, 0, 0);
但是我得到其他错误:
math3d.cpp:15: error: invalid use of incomplete type ‘class Vector3’
math3d.cpp:9: error: forward declaration of ‘class Vector3’
math3d.cpp:15: error: invalid in-class initialization of static data member of non-integral type ‘const Vector3’
我班上的重要组成部分,到目前为止是这样的
class Vector3
{
public:
double X;
double Y;
double Z;
static Vector3 Xaxis = Vector3(1, 0, 0);
Vector3(double x, double y, double z)
{
X = x; Y = y; Z = z;
}
};
如何实现我想在这里做什么?要有一个Vector3 :: Xaxis,它返回Vector3(1.0,0.0,0.0);
[C++在哪里初始化静态常量]可能的重复(http://stackoverflow.com/questions/2605520/c-where-to-initialize-static-const) – Troubadour