更新:2007 年 11 月
错误消息
“成员”: 静态数据成员无法通过派生类初始化
静态数据成员的初始化的格式错误。编译器已接受该初始化。
这是 Visual C++ .NET 2003 编译器中的重大更改。有关更多信息,请参见编译时的重大更改摘要。
为使代码在 Visual C++ 的所有版本中以相同方式工作,通过基类初始化成员。
使用 warning 杂注取消此警告。
下面的示例生成 C4356:
// C4356.cpp
// compile with: /W2 /EHsc
#include <iostream>
template <class T>
class C {
static int n;
};
class D : C<int> {};
int D::n = 0; // C4356
// try the following line instead
// int C<int>::n = 0;
class A {
public:
static int n;
};
class B : public A {};
int B::n = 10; // C4356
// try the following line instead
// int A::n = 99;
int main() {
using namespace std;
cout << B::n << endl;
}