Archived

This topic is now archived and is closed to further replies.

Elwren

Header Files

Recommended Posts

Now I''ve always been under the assumption that I should put all my class declarations in a .h file and put the definition of the class in a seperate .cpp file. Is there a certain reason for this? Personally, I think having the declaration and definition in the same file makes it easier to edit/find bugs. Just wondering what people''s views are on this...thanks

Share this post


Link to post
Share on other sites
quote:
Original post by Elwren
Now I''ve always been under the assumption that I should put all my class declarations in a .h file and put the definition of the class in a seperate .cpp file. Is there a certain reason for this? Personally, I think having the declaration and definition in the same file makes it easier to edit/find bugs. Just wondering what people''s views are on this...thanks


Well, having the declaration in one file and the definition in another file is just for ease of re-use and also, it''s easier to debug a complete project if the declaration and definitions are in seperate files IMHO.



"And that''s the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer

Share this post


Link to post
Share on other sites
Well if you put all of your declarations in your .cpp file then only your .cpp can use those functions. You can create a class declaration and keep the declarations in the same file, but your class is useless unless your entire program is in that same file (including main or WinMain).

You can of course cut and paste all of the declarations into your other CPP files, but then if you need to change something you will have to change it in all of the files that you have the declarations in. Most OO projects that go beyond Hello World and spinning cubes make use of the same class in many many different modules. Not seperating prototypes from code would be madness in these situations and will definitely not make it easier to find bugs. Quite the contrary.

And if you want to compile your code into a library you absolutely MUST have the prototypes in a header file or how would anybody use the library?

Seeya
Krippy

Share this post


Link to post
Share on other sites
You sure can, that''s how Java does it.

The only thing is, your program will take forever to compile. Imaging having a 20,000 line program and all the classes are in one file (or a bunch of files included by the main file). It''d take 1/2 hour to compile, even if you just make one little change!

If you put the declaration in one file and the definition in another, if you make a change to the definition, you don''t need to re-compile the whole project - just the one file.


War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites
It also means you have to do all sorts of code juggling to ensure that code is in such an order that it always comes after its dependencies. And if you have cyclic dependencies of any type, that''s a nightmare. Better to separate them all out.

Share this post


Link to post
Share on other sites
Well, in Java it''s not that bad (or even close) for two reasons... First, it uses a mult-pass compiler so that declaration order and cyclic dependencies don''t matter. Second, it loads classes dynamically, so changing one class only requires recompiling those that depend directly upon the parts of its implementation that were changed.

Since C and C++ were designed for a one pass compiler, and the object files are statically linked, it just isn''t something that should be done in those languages.

Share this post


Link to post
Share on other sites