home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 16069 < prev    next >
Encoding:
Text File  |  1992-11-10  |  2.7 KB  |  69 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.tek.com!uw-beaver!news.u.washington.edu!news.uoregon.edu!cs.uoregon.edu!sgiblab!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!hellgate.utah.edu!fcom.cc.utah.edu!swillden
  3. From: swillden@news.ccutah.edu (Shawn Willden)
  4. Subject: Re: Multiple Header Files Problem
  5. Message-ID: <1992Nov11.015747.20842@fcom.cc.utah.edu>
  6. Sender: news@fcom.cc.utah.edu
  7. Reply-To: swillden@icarus.weber.edu
  8. Organization: University of Utah Computer Center
  9. X-Newsreader: Tin 1.1 PL3
  10. References: <rmartin.721442494@thor>
  11. Date: Wed, 11 Nov 92 01:57:47 GMT
  12. Lines: 55
  13.  
  14. rmartin@thor.Rational.COM (Bob Martin) writes:
  15. : reindorf@us-es.sel.de (Charles Reindorf) writes:
  16. : |In article <rmartin.721359424@thor>, rmartin@thor.Rational.COM (Bob Martin) writes:
  17. : ||> A more interesting variation on this issue is the problem of "circular
  18. : |It is usual, in header files, to structure them so :
  19. : || # ifndef <HeaderFileName>_h
  20. : || # define <HeaderFileName>_h
  21. : || ... body of include file ...
  22. : || # endif
  23. : |This prevents cyclic dependencies and has been in common practice for, well, ages.
  24. : Of course!  And of course I use this technique myself, and instruct my
  25. : students to do the same. 
  26. : However, using the #ifndef convention does not solve all the problems
  27. : related to inclusion, nor obviate the need to make judicious use of
  28. : forward declarations.  #ifndef, while it *does* keep the contents of a
  29. : header file from being compiled more than once, does not keep the
  30. : header file from being *read* more than once.  Thus, if care is not
  31. : taken, compile times can grow quite long.
  32. : Moreover, some compilers limit the number of files that they will
  33. : include, or at least the depth of the inclusions.  
  34. : So, take care when using #include in a header file.  Always be sure
  35. : that you the #include is necessary, and that it cannot be replaced
  36. : with a forward declaration.
  37.  
  38. While I agree with that it is a good idea to choose your #included
  39. files carefully, the above-metioned problems are also easily avoided
  40. by simply wrapping each inclusion in its own #ifndef...#endif pair.
  41. Like:
  42.  
  43. #ifndef SOMEFILE_H
  44. #include "somefile.h"
  45. #endif
  46.  
  47. That way, if somefile.h has already been included once, the compiler
  48. won't even have to read it again.  If this technique is always used
  49. the #ifndef..#endif pairs are not even needed in the included file,
  50. though I always use them, just in case someone else makes a mistake
  51. (something I'm sure *I* would never do :-)
  52.  
  53. : --
  54. : Robert Martin                        Training courses offered in:
  55. : R. C. M. Consulting                       Object Oriented Analysis
  56. : 2080 Cranbrook Rd.                        Object Oriented Design
  57. : Green Oaks, Il 60048 (708) 918-1004       C++
  58.  
  59. --
  60. Shawn Willden
  61. swillden@icarus.weber.edu
  62.