question about file organization

Hi, all -

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?

Any suggestions are welcome. Thanks.
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.

OK, so no function declarations in the header files at all, then? That would make things a bit simpler, I guess.
You got it =D

The compiler will always complain if you include the header file in more than one place if you have full function declarations inside said file.
Another question: I'm hoping to eliminate (or at least minimize) the need for a globals.h file, but I will have the need for something like this:

const int LOW=0;
const int HIGH=1;

Any recommendations? If I put it in a globals.h file, and access it in multiple locations, I'll get linker errors, right?
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
1
2
3
4
5
6
7
8
//header.h

#ifndef HEADER_H
#define HEADER_H

//prototypes, etc

#endif 

Last edited on
You can put as many (const) global variables as you desire inside a header file, don't worry about multiple definitions.
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.
Last edited on
But why do you need globals? All the objects need to know about that globals?
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?

Thanks.
if possible, i would write a function, that when called returns the state of the clock
Topic archived. No new replies allowed.