# including header file issues and global pointers

## Recommended Posts

I'm having trouble figuring out the best way to include header files in my project. If I have multiple classes, and each one of them requires the same header files, like d3d9.h, d3dx9.h, iostream, etc., should I be putting these #include statements in every file that requires them? In one directx book I've been reading, all common header files were put in an Engine class and all other classes that required the same header files just included the Engine class. Is this an ok approach? If the Engine class includes a lot of header files, and another class only needs one of them, isn't that going to be a waste to include the whole Engine class? Also, every class that includes the Engine would no longer be reusable in other projects because all its' required header files are included through the Engine class. The book also declared an extern global pointer to the Engine so that the Engine's direct3d device wouldn't have to be passed as a parameter into every class that requires it. Is it better to do this or just pass the pointer into every class where it is needed? You would then have to keep track of all the pointers right? How is everyone else handling these issues? Is there a better way to solve this? Thanks in advance!

##### Share on other sites
The header files you are including are only included once, even if you do the include statement multiple times in the code.

That is because they are doing something like this

#ifndef _d3d9_H_#define _d3d9_H_Some DirectX code here...#endif

This means if the include file already been included, _d3d9_H_ has already been defined and thus skipping the code.

You should use a similar approach for you own header files.

I always include the libraries I use in each file, because then I know it is included and it also improves the readablility of your code, knowing what libraries you are using.

##### Share on other sites
Also, in Project Properties -> C++ -> Advanced, you can turn on Show Includes which really helps getting your head around the crazy looking errors when you get circular references etc.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628288
• Total Posts
2981846

• 11
• 10
• 10
• 11
• 17