![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 24
| Trying to make the best use of header files I am now considering putting all declarations in the header file corresponding to the source file where the definition is found, and include that header in all source files that need to call one of those functions. I'll still use the global header for the declarations that are so widely used that I can't seem to keep track of who wants them...or I could include such individual headers in the global header file rather than pull the individual declarations across. Does this sound like a plan? |
| alanb is offline | |
| | #2 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Sure. Until you get into more complicated stuff, having a corresponding .h file for every .cpp file is a great idea. You just put the function prototypes and so on that are defined in the .cpp file in the appropriate header file. You'll probably also end up wanting to create, for example, somesubsystem.h, which includes a bunch of header files that you commonly use together . . . but be careful not to overuse this sort of thing. At the least, it means you'll have to wait a long time for recompilation if you modify a commonly-used header. And it might make the code harder to modify in the future . . . .
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #3 |
| Ex scientia vera Join Date: Sep 2007
Posts: 464
| Generally, trying to include only the header files that contain declarations/definitions for what you actually need in your code is best practice, I believe. This also reduces compile time when you get larger projects, as long as you are using a makefile or some such. It will only compile the files that have been modified. Knowing a bit more about your program and how you are structuring it might help people with giving you some advice.
__________________ "What's up, Doc?" "'Up' is a relative concept. It has no intrinsic value." |
| IceDane is offline | |
| | #4 |
| Registered User Join Date: Aug 2009
Posts: 24
| OK, so I gather that global header files should be used judiciously and preferrably only semi-globally. Say, I want to build a world. I have one file for starting and getting a window, one for initialising the graphics system, one for rendering - which has everything from model definitions to projection (may eventually break this into two), and one for movement which has everything from keyboard input to physics calculations (may also break this one up in the future). |
| alanb is offline | |
| | #5 | ||
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Quote:
Quote:
Once you start getting a lot of code, you might even want to break up your code into different subdirectories; graphics/, gui/, network/, etc. Then you can put code in each directory into a namespace of the same name. But you shouldn't have to worry about that for a while.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. | ||
| dwks is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multiple Source Files, make files, scope, include | thetinman | C++ Programming | 13 | 11-05-2008 11:37 PM |
| Help using Header Files | d34n | C Programming | 8 | 04-21-2008 11:06 PM |
| added start menu crashes game | avgprogamerjoe | Game Programming | 6 | 08-29-2007 01:30 PM |
| What /what not to put in header files | Just | C Programming | 1 | 12-14-2002 10:45 AM |
| Header files | Jez_Master | C++ Programming | 2 | 04-08-2002 07:56 AM |