我有一个全球性的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