Skip to main content
gamedev.net
🔒 Locked

.h or .cpp theory

Started by bzroom Apr 24, 2003 at 6:10 PM 5 replies 500+ views
Original Post
bzroom
bzroom
how come when im looking through tutorials i always see the class declarations in a header file, and then the actualy functions in a .cpp file, what is the theory behind this and is it ok/advised if i just simply put both the declaration and functions in a header? thanx
duhroach
duhroach
In order to call a function or use a variable in C++, you have to have it defined somewhere. Seperate .cpp files have no knowlage of eachother, therefore, if you want to call a function in a seperate .cpp file, you must include the header file with the definition (Function prototype) so that the current file knows where to locate that call. If you didn''t have header files, then all code would be in one .cpp file. Which is very messy.

As for the theory aspect of it, if you''ve ever used an older language, like MODULA, Pascal, Fortran, or even ADA, you''d appriciate the ability that C++ gives for easily dividing up code so that everything isn''t so clumped together and "messy"

Hope that helped.
~Main

==
Colt "MainRoach" McAnlis
Programmer
www.badheat.com/sinewave
chiuyan
chiuyan
long compile times, much rebuilding, multiply defined symbols, big executables, etc.

If you put everthing in the header file, and you decide to change a function, then you have to change the header file, and everyone who includes that header file has to be rebuilt. It takes about 20 minutes to rebuild all for our game for PS2, you want to minimize rebuilding as much as possible.

also, the #include macro (directive, whatever) just copies and pastes everything in the .h file into the .cpp that includes it. This means that every .cpp that includes that file has its own copy of everything in the .h file. This leads to large executables, and possibly multiply defined symbols (which will break your compile).

Only things that have to be in the .h file should be in the .h file, IMHO. Everything else should be in the .cpp. I would even advise that you go out of your way to keep stuff out of the .h file, by forward declaring things, and putting inlined functions in .inl files (or exporting them if your compiler supports that--I don''t think any do yet.)
noVum
noVum
quote:
Original post by chiuyan
(or exporting them if your compiler supports that--I don''t think any do yet.)


Comeau C++ does
bzroom
bzroom
ok i read that page, the thing i dont'' like is after i ahve created my headers all i have to do to include those functions is just include that header, if i put the functinos in a .cpp i ahve to first include that cpp into my project, and include the header into my main cpp file correct? these are project specific headers, the reason why i don''t just put all the code in one cpp is the obvious to much code too little space disorginization problem, these are not being distributed, thanx for your replys
bzroom
bzroom
the rebuild time thing just clicked in, i was thinking what are they talking about, as long as i don''t change the header file then it will just rebuild the cpp that includes it, but including is liek rebuilding it since it is coping it in, so if theres only 10 lines in the header file it will compile much faster (with that headeres cpp file alreayd compiled) then if all the functions and everythign were in the header it self. ok.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.