Sign in to follow this  

.h files in C++

Recommended Posts

Thank, just one more question.

Lets say that I create a game in allegro (something like metroid).

Can I keep all the controls, sprites, sounds, etc. for the main character in a header file (ex. "samus.h") with custom functions (like "jump()", "shoot()", "shoot.charge()" that work with the allegro game library.

And then one for the enemies (ex. "enemies.h") with similar properties to "samus.h".

And then link it it all together into "main.cpp"?

Share this post

Link to post
Share on other sites
Original post by In_Yack_Mode
You could do that stuff, but headers are for declarations. You use .cpp files for the actual code. So if you have someFunction then you declare it in someFunction.h and implement it in someFunction.cpp.

Not strictly true; Headers will have definitions for inline functions and templates.

Good use of headers can be very useful when your project grows in size, my recommendation is to read as much as you can about it; that link SiCrane supplied is a good start, its not the most exciting aspect of C++ but it can save you alot of headaches.

You should follow In_Yack_Mode's advice but remember its not always the case.

Also when you start making bigger projects seriously consider having headers with forward declarations only; I'm going to try this more in my own C++ projects going forward. Anything to allow better code organisation and break up dependencies between differing translation units is good for development and shortens re-compile times :)

I'm still finding my feet somewhat in this area so don't take my word as gospel just want to add my thoughts :)

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