0

i got compiler error stating that :

Only static data members with const integral or const enumeration type can specify an initializer in the class definition

what is the logic behind that? why is the compiler designed not to accept any non constant static initialization of types?

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Narue
0

>why is the compiler designed not to accept
>any non constant static initialization of types?
You can (technically) initialize static data members with non-constant values, just not directly within the class definition:

#include <cstdlib>
#include <iostream>

class foo {
public:
  static int x;
};

int foo::x = rand();

int main()
{
  std::cout<< foo::x <<'\n';
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.