Sign in to follow this  
King_DuckZ

Headers including other headers

Recommended Posts

Hello everyone, I've been having this problem for quite a while, and I'd like to hear what other people think about it. It often happens, when declaring a class, that it uses a pointer or a reference to some class, struct or union declared somewhere else. Using predeclaration most of the times does the trick, but sometimes it's not enough. An example: I'm writing an input management class, using SDL. I need to store a private pointer to the SDL_Joystick struct, which is actually a typedef to _SDL_Joystick. Predeclaring the typedef won't work, and I don't want to predeclare _SDL_Joystick, so I'm forced to include the whole SDL.h whithin my .h file. The same happens with stl stuff. Leaving alone compiling time, and assuming that there is a precompiled header already including SDL.h first so that this second inclusion is getting "pragmaonced" out, is there any good reason for which I shouldn't include potentially big headers in my headers, or am I just being maniacal?

Share this post


Link to post
Share on other sites
Quote:
Original post by King_DuckZ
An example: I'm writing an input management class, using SDL. I need to store a private pointer to the SDL_Joystick struct, which is actually a typedef to _SDL_Joystick. Predeclaring the typedef won't work, and I don't want to predeclare _SDL_Joystick, so I'm forced to include the whole SDL.h whithin my .h file. The same happens with stl stuff.


Did you try:

struct _SDL_Joystick;
typedef _SDL_Joystick SDL_Joystick;

?

Quote:
Original post by King_DuckZ
Leaving alone compiling time, and assuming that there is a precompiled header already including SDL.h first so that this second inclusion is getting "pragmaonced" out, is there any good reason for which I shouldn't include potentially big headers in my headers, or am I just being maniacal?


You should probably try the Pimpl idiom.

Share this post


Link to post
Share on other sites
Nope, I will try right now, I think it's a better idea than including the whole header anyways. Thank you for the link, I didn't know there were official "best practices" to follow, and I found it very interesting!

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