I’ve recently run into Visual C++ 2005 failing to initialize in class constants, having run into the ubiquitous error
“error C2864: … : only static const integral data members can be initialized within a class”
from code similar to
class MyClass:
{
private:
static const double myConstant = 2.9768;
}
I’ve been able to figure out that non-integer types are the problem, and there are several ways to have integer constants, but I have not found a satisfactory work-around for defining constants scoped to a class. Is this type of declaration legal in later/other compilers?
Do it outside the class definition, in a source file (not a header, or you risk linker errors).
const double MyClass::myConstant = ..;This behaviour is mandated by the C++ language standard. No legal workaround.