class foo {
static const string s; // Can never be initialized here.
static const char* cs; // Same with C strings.
static const int i = 3; // Integral types can be initialized here (*)...
static const int j; // ... OR in cpp.
};
Foo.cpp
#include "foo.h"
const string foo::s = "foo string";
const char* foo::cs = "foo C string";
// No definition for i. (*)
const int foo::j = 4;
(*)根据标准,如果在代码中使用 i而不仅仅是整数常量表达式,则必须在类定义之外定义 i(就像 j一样)。有关详细信息,请参阅下面 David 的评论。