是否需要单独定义C++中的类或结构的常量静态成员变量?
它是否正确?
struct test
{
const static int x;
};
int test::x;
Run Code Online (Sandbox Code Playgroud)
不,这不正确.定义必须声明匹配,并且x是const int不是int.作为constPOD类型的变量,它还需要初始化.例如
const int test::x = 0;
Run Code Online (Sandbox Code Playgroud)
作为const static整数类型的成员,也允许在类的定义中提供初始化器.