我有一個全球性的int
我想在不同的文件改變,由於某種原因,這是行不通的。全局變量不起作用
我:
//test.h
#include <windows.h>
static int start1; //want to use this globally.
//declare
void something();
//test.cpp
#include "test.h"
extern int start1;
void something()
{
start1 = start1 + 1;
}
//main.cpp
#include "test.h"
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
start1 = 3;
something();
return 0;
}
爲什麼時,你去something()
是start1
0,而不是3?我一直試圖讓一個全局變量在幾個小時內,而且它不起作用。請有人澄清?
但實際情況並非test.h和main.cpp中都在同一個翻譯單元反正? – 2012-02-23 16:03:38
@TonyTheLion:'test.h'被多個源文件包含,因此會影響多個翻譯單元。 – 2012-02-23 16:04:49