I'm writing my first C++ program for a real-world application right now, and am trying to figure out a good scheme for what to put in which files.
What I'm wrestling with right now is what to put in .h files, and what to put in .cpp files. Specifically, I remember reading somewhere that a good rule of thumb is that if you can put your function on one line (like an empty destructor), to put it in the .h file; otherwise to put it in the .cpp file.
But, if I do this, and I use the include file in more than one place, won't the linker object about multiple definitions of a function?
Well what you really want to do is put all of your function prototypes in your header file. In the corresponding cpp file, put the full declarations. Same with classes, put the declaration of your class in the header file, however be sure to also include your variables in the declaration and include method prototypes as well.
my preference is to put only function prototypes and any struct/class/etc. definitions in the header file and any function definitions in the .cpp file. my rule of thumb is that if i have code that's going to create anything in memory, it goes in a .cpp file
if you're getting multiple inclusion errors, make sure you're telling it to include the library only once
OK. So, would it be desirable to create a globals.cpp to contain the actual globals, and a globals.h to declare them as extern? I hate to have to maintain two files, but I'll do it if necessary.
Alternatively, I could create classes for the global information I need to store, but that too sounds like overkill.
No, not really. You could do that, but the benefit of doing so is rather eh, not so big. As long as you have a header guarder, you can just leave it as it is, there is no real gain from defining the globals in a seperate file. And yes, it wouldn't be just overkill, it would be completely pointless to create classes for just your globals. Though you should give putting them into a seperate namespace a thought if you are afraid of name collisions.
The program I'm writing is a simulator of an ASIC. The chip has a system clock. It seems natural to put a variable to represent the current clock state (HIGH or LOW) into a global variable. Even if I create a clock object instead, though, it would be nice to put the definitions for HIGH and LOW in a single file for inclusion everywhere.
So, hanst...for the example I just gave above, what would your recommendation for files be? Where would the clock variable, and the HIGH and LOW constants be defined/declared?