non-local static 对象初始化的顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// A.h, no header guard
#include <iostream>
class A {
public:
int n;
A(int i) : n(i) { std::cout << "A(" << n << ")" << std::endl; }
int f() { return n; }
};

// A.cpp
#include "A.h"
extern A a;
A a(100);

// B.cpp
#include "A.h"
extern A a;
class B {
public:
B() {
std::cout << "B()" << std::endl;
std::cout << a.f() << std::endl;
}
};
B b;
int main() {}
1
2
3
4
5
6
7
8
$ g++ A.cpp B.cpp && ./a.out
A(100)
B()
100
$ g++ B.cpp A.cpp && ./a.out
B()
0
A(100)

不同翻译单元内 non-local static 对象初始化的顺序是不确定的,使用 Singleton 解决此类问题

1
2
3
4
A& GetA() {
static A a(100);
return a;
}