How should global variables be handled when using seperated sourcefiles?
I found you need to declare the variable again using the keyword "extern" when you need access to a global variable declared in another sourcefile. I'm to lazy to repeat that for each sourcefile in my project, so I'm using the following construction:
1 2 3
//global_variables.cpp
int global_var;
1 2 3
//global_variables.h
externint global_var;
1 2 3 4 5
//sourcefile.cpp
#include "global_variables.h"
/* use the variable somewhere */
I had a teacher "so and so" tell me not to include inside header files and I think I even read this in a C++ text but I really do like the method in this article. It seems like it will help more in the OOP approach and will help make the code more robust! Thanks for the article...
I don't understand this part of the discussion. It is impossible not to include headers inside of other headers in all cases. What does the teacher expect you to do, dynamically allocate every single object in the entire system on the heap?
You obviously have to have a strategy and include as much as possible within .cpp files. I think that it would be very difficult and unwise to never include headers within headers in a large scale app.
Yes, that's exactly right. That used to be a paradigm some folks kept to when developing C software. A company I used to work for tried at some level to adhere to that. It basically meant you could never add anything to a "common" header file that would suddenly require every .c file to include a new header.
I agree, though, in a C++ world it makes no sense at all.
(Ok, even in a C world it still doesn't make sense to me.)