The following example is from C++ Standard(ISO14882)
9.4.2 Static data members
class process {
static process* run_chain;
static process* running;
};
process* process::running = get_main();//what does get_main() mean here?
process* process::run_chain = running;
C++ Standard also says in:
5.19 Constant expressions
In several places, C + + requires expressions that evaluate to an
integral or enumeration constant: as array bounds , as case
expressions , as bit-field lengths , as enumerator initializers ,as
static member initializers, and as integral or enumeration non-type
template arguments.
get_main() is not constant expression,is it? How can it be a "static
member initializer"?
WHEN is static data member is initialized?
The following two code snippets puzzle a lot of people.
1 #include <iostream>
2 using namespace std;
3 class a{
4 public:
5
6 static int i;//declaration
7 };
8 int f(){ return a::i+7;}
9 int a::i = f();//definition herer
10
11 int main()
12 {
13 a Aa;
14 cout << Aa.i;
15 }
//The output is 7 on gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
************************************************** **********************************************
1 #include <iostream>
2 using namespace std;
3 class a{
4 public:
5
6 static int i;
7 };
8 void f()
9 {
10 cout<<a::i<<endl;
11 }
12 int a::i = 9;
13
14 int main()
15 {
16 f();
17 }
//The output is 9 on gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)