Look at the code below, this const static int num1 = 8;
compiles. However, the const static double num2 = 8.8;
code gives an error. Their modifiers are all const static
, so why does int
work, but double
and other floating-point types result in an error?
struct S {
const static int num1 = 8; // OK
const static double num2 = 8.8; // error
};
I've tried some built-in types of C++ and found that
- integer members such as
int
,short
,long
, andchar
can be defined withconst static
in the class and directly initialized, while - floating-point members such as
float
anddouble
can not.
This confuses me a lot.