Header file placement.

Started by
3 comments, last by Khatharr 10 years, 10 months ago

Language is C++.

Ok, so their is no such thing as a stupid question..

I know the Name says it all with "Header File", but does it matter where I include the header file - I am using header files to contain my different classes, E.G Players.h, MapBuilder.h, GameLogic.h.

You can pretty much guess what each header contains. Now because the Players.H file uses Global variables from my main .cpp I have #include the header file after the variables have been initialized. Will this cause me problems later down the line and if it will can someone point me in the direction of a better solution?.

Cheers Rob

~~~~~~Please remember I am posting in the beginner forum for a reason - so please don't flame~~~~~~

Advertisement

1. Dont use globals if you can use non-globals

2. If you do, put the globals in a header file that the player includes and initialize them in some cpp file (not exactly sure how to do this, googling will tell you. Or you can pack them in a struct or class and pass one to player if it makes sense)

So youd declare the globals in a .h, and define them in a .cpp (so you have 2 .cpp, one is main and other is globals? or you can define them in main too possibly)

o3o

Extending a bit on what Waterlimon says, a couple things to keep in mind:

In regards to Water's comment #1, don't get too hung up on that. As a beginner, you are likely to figure globals are fine for your uses, it will likely take a bit of pain to learn why they are bad. In general though, do try to avoid them, but don't worry too much about it right now.

Rephrasing Water's #2 a bit, you never define your globals in headers unless they are const or preprocessor definitions. You *declare* your globals in headers via:

extern int MyGlobal;

And then in a c/cpp file you actually define it as:

int MyGlobal = 10;

Or, if they are const, then any of the following are valid in headers:

#define MY_GLOBAL 10
const int MyGlobal = 10;
enum { MyGlobal = 10 };
 
class someclass
{
public:
  static const int MyGlobal = 10;
};

There are many other variations that I'm probably missing but generally for a global you intend to change from multiple locations, you want the first mentioned variation. You'll learn why that's not a great idea pretty quickly though, global state is generally a bad idea unless under strict control. (I.e. suggestion is to at least make a global class to wrap your globals and use accessors so that when you end up with bugs, you can insert logging code to find who is setting things badly..)

thanks for the great comments guys. I'm going to spend a little time tonight going through information regarding putting globals in header files.

The basic idea for all header files is that they don't make things: they only describe things that are made elsewhere. Like Eight mentioned, learning the difference between declaration (there's something) and definition (and this is what it is) is the key to understanding this.

For instance:


/*FUNCTIONS*/
int myFunc(int foo, char* bar); //declaration

int myFunc(int foo, char* bar) { //definition
  return bar[foo];
}

/*CLASSES*/
class Thingy { //declaration
public:
  Thingy();
private:
  int number;
};

//definition
Thingy::Thingy() {
 number = 4;
}

/*GLOBALS*/
extern int myGlobalVar; //declaration (extern here means 'defined elsewhere')

int myGlobalVar = 5; //definition (assigning a value here is optional)

/*NAMESPACE MEMBERS*/
namespace ThePlace { //declaration (same as in the global space)
  extern int itsReallyJustAGlobal;
  int aFunction();
}

int ThePlace::itsReallyJustAGlobal; //definition

int ThePlace::aFunction() { //definition
  return 3;
}

Declarations go in header files. Definitions go in code files. The reason is that declarations are information for the linker, which connects the parts of your program together. Definitions are information for the compiler, which actually makes the parts. Declaration allows you to tell all of the parts of your program that something exists, but you only have to define the part once.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement