i am trying to update static int
test.h
static int Delay = 0;
void UpdateDelay();
test.cpp
#include "test.h"
void UpdateDelay(){
Delay = 500;
}
Main.cpp
#include "test.h"
int main(){
UpdateDelay();
std::cout << Delay << std::endl;
return 0;
}
output should be : 500
but it shows : 0
Thx
A global variable declared as
statichas internal linkage. This means that each translation unit (i.e..cppfile) gets a private copy of that variable.Changes done to a private copy of one translation unit won’t have any effect on private copies of the same variable held by different translation units.
If you want to share one global variable, provide one definition for it in a single translation unit, and let all other translation unit refer it through a declaration that specifies the
externkeyword:test.h
test.cpp
main.cpp