开发者

C++ header files and variable scope

开发者 https://www.devze.com 2023-01-01 03:40 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消