KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Why can't I initialize non-const static member or static array in a class? class A { static const int a = 3; static int b = 3; static const int c[2] = { 1, 2 }; static int d[2] = { 1, 2 }; }; int main() { A a; return 0; } the compiler issues following errors: g++ main.cpp main.cpp:4:17: error: ISO C++ forbids in-class initialization of non-const static member ‘b’ main.cpp:5:26: error: a brace-enclosed initializer is not allowed here before ‘{’ token main.cpp:5:33: error: invalid in-class initialization of static data member of non-integral type ‘const int [2]’ main.cpp:6:20: error: a brace-enclosed initializer is not allowed here before ‘{’ token main.cpp:6:27: error: invalid in-class initialization of static data member of non-integral type ‘int [2]’ I have two questions: Why can't I initialize static data members in class? Why can't I initialize static arrays in class, even the const array?
Tags (comma-separated)
Save Edits
Cancel