最近在coding的时候,写了这么一段代码:
A.h
1
// A.h
2
#include <map>
3![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
4
class A
5![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6
public:
7
static std::map<int, int> x;
8
};
A.cpp
1
// A.cpp
2
#include "A.h"
3![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
4
std::map<int, int> Test::x;
很简单,也很正常,看看用法吧。
main.cpp
1
class B
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
public:
4
B();
5
};
6![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
7
class C
8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
public:
10
static B b;
11
};
12![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
13
B C::b;
14![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
15
B::B()
16![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
17
A::x[1] = 2;
18
}
19![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
20
void main()
21![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
22
A::x[3] = 3;
23
}
好了,按照上面的写法,把代码分别放到3个文件里面,编译,通过, 链接,通过,运行,挂了!!why?
解决方案很简单,把A.cpp里面的那句定义放到main.cpp里面就好了。
问题在于两个static变量分别在两个不同的文件里,A.cpp 里面那static变量要在main.cpp前初始化才行,可是编译器不知道呀,结果main里面的先初始化了,等到调用A.cpp里面那个变量时,已经来不及啦。
这个就是
依赖编译器初始化顺序的程序,如果有人运行没有挂,那恭喜啦,等到它挂的时候你可要哭了。
上面的解决方案太烂了,如果程序规模很大,就完了,比如有好几个cpp里面要用到A.cpp里面那个变量,放哪个里面好呢?
这时候,Scott Mayer的Singleton就是解决方案啦,它能保证静态变量在使用前被初始化。
看看下面的代码:
1
class static_
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
public:
4
template <int N, class T>
5
static T& var()
6![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
static T instance;
8
return instance;
9
}
10![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
11
private:
12![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
~static_()
{}
13
};
有了这个定义,想用静态变量的时候就爽啦~~,用的时候:
1
static_::var<0, std::string>() = ":)";
2
static_::var<0, std::map<int, int> >()[0] = 3;
3
static_::var<1, std::string>() = "haha";
现在的效果就是,想让Test里面有什么,他就有什么,我指静态变量。注意啦,第一个模板参数是为了可以定义同种类型的多个变量,就相当于
var_0, var_1...
是不是感觉有点动态语言的特性了,变量不用定义就可以用。而且变量初始化顺序也不用关心啦,因为所有的变量会在使用前初始化完毕。
如果和boost::function合作起来,效果更佳。look!
1
class Fun
2![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
public:
4
Fun()
5
: UId(&static_::var<0, std::string>)
6![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
}
8
// interface for static variable.
9
boost::function<std::string& (void)> UId;
10
};
11![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
12
void main()
13![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
14
Fun f1, f2;
15
// set static from f1
16
f1.UId() = "abc";
17
// get static from f2
18
std::string str = f2.UId();
19
}
好了,static就到这里吧。
posted on 2009-03-20 14:16
尹东斐 阅读(4627)
评论(5) 编辑 收藏 引用