How do I declare static constant values in C++? I want to be able to get the constant Vector3::Xaxis, but I should not be able to change it.
I\'ve seen the following cod
You need to initialize static members outside of class declaration.
class Vector3
{
public:
double X;
double Y;
double Z;
static Vector3 const Xaxis;
Vector3(double x, double y, double z)
{
X = x; Y = y; Z = z;
}
};
Vector3 const Vector3::Xaxis(1, 0, 0);
Note that last line is the definition and should be put in an implementation file (e.g. [.cpp] or [.cc]).
If you need this for a header-only module then there is a template-based trick that do it for you – but better ask separately about that if you need it.
Cheers & hth.,