我在C++中的一些概念中变得非常困惑。对于例如:我有以下两个文件静态初始化混淆
//file1.cpp
class test
{
static int s;
public:
test(){s++;}
};
static test t;
int test::s=5;
//file2.cpp
#include<iostream>
using namespace std;
class test
{
static int s;
public:
test(){s++;}
static int get()
{
return s;
}
};
static test t;
int main()
{
cout<<test::get()<<endl;
}
现在我的问题是:
1.如何两个文件成功链接,即使他们有不同的类定义?
2.两个类的静态成员是否相关,因为我得到的输出为7.
请解释这个静态的概念。
Ok..Now假设我写在同一个类定义两个文件。 那我的第二个问题呢? 这两个类的静态成员是相关的吗? – 2010-06-14 10:31:14
@Happy只能有一个名称为test的类,所以只能有一个静态变量。其他任何东西都是连接器产生的人工制品。 – 2010-06-14 10:33:18
我正在使用Bruce Eckel的C++中的Thinking。其实我正在阅读schwarz计数器技术的静态初始化命令失败,但我无法理解为解释该技术而编写的代码。 如果你有这本书,你能解释一下这个代码吗? – 2010-06-14 10:34:27