Sign in to follow this  

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 this post

Link to post
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...


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 this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this