C++ header files and variable scope
I want to organize my c++ variables and functions in the following way: function prototypes in a header file "stuff.h", function implementation in "stuff.cpp", then say #include "stuff.h" in main.cpp (so I can call functions implemented in stuff.cpp). So far so good. Now I want to declare some variab开发者_运维百科les in stuff.cpp that have global scope (so I can modify the variables in functions implemented in stuff.cpp and main.cpp). This doesn't seem to work. How can I do this?
Declare them as extern. E.g., in stuff.h:
extern int g_number;
Then in stuff.cc:
int g_number = 123;
Then in main.cc just #include stuff.h
.
精彩评论