我在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