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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!thor!rmartin
  3. From: rmartin@thor.Rational.COM (Bob Martin)
  4. Subject: Re: Multiple Header Files Problem
  5. Message-ID: <rmartin.721442494@thor>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <720993360snx@trmphrst.demon.co.uk> <rmartin.721359424@thor>  <1992Nov10.104447.12882@us-es.sel.de>
  9. Date: Wed, 11 Nov 1992 00:41:34 GMT
  10. Lines: 57
  11.  
  12. reindorf@us-es.sel.de (Charles Reindorf) writes:
  13.  
  14. |In article <rmartin.721359424@thor>, rmartin@thor.Rational.COM (Bob Martin) writes:
  15. ||> A more interesting variation on this issue is the problem of "circular
  16. ||> 
  17. ||> [ etc.]
  18.  
  19. |It is usual, in header files, to structure them so :
  20.  
  21.  
  22. || # ifndef <HeaderFileName>_h
  23. || # define <HeaderFileName>_h
  24. || 
  25. || 
  26. || ... body of include file ...
  27. || 
  28. || 
  29. || # endif
  30.  
  31. |This prevents cyclic dependencies and has been in common practice for, well, ages.
  32.  
  33. Of course!  And of course I use this technique myself, and instruct my
  34. students to do the same. 
  35.  
  36. What I had not realized, until you (and many others) pointed it out to
  37. me, is that the #ifndef convention breaks inclusion cycles.  Of course
  38. it does.
  39.  
  40. I have run into inclusion cycles several times in the past few years.
  41. The first one I ran into, was of my own creation.  This was before I
  42. knew about the #ifndef convention.  I discovered that convention at a
  43. separate time, and used it to solve a different problem.  So the two
  44. never gelled together in my brain.  The other cycles I have stumbled
  45. accross were written by clients of mine who were just starting at C++,
  46. and had not yet learned all the conventions.
  47.  
  48. However, using the #ifndef convention does not solve all the problems
  49. related to inclusion, nor obviate the need to make judicious use of
  50. forward declarations.  #ifndef, while it *does* keep the contents of a
  51. header file from being compiled more than once, does not keep the
  52. header file from being *read* more than once.  Thus, if care is not
  53. taken, compile times can grow quite long.
  54.  
  55. Moreover, some compilers limit the number of files that they will
  56. include, or at least the depth of the inclusions.  
  57.  
  58. So, take care when using #include in a header file.  Always be sure
  59. that you the #include is necessary, and that it cannot be replaced
  60. with a forward declaration.
  61.  
  62.  
  63.  
  64. --
  65. Robert Martin                        Training courses offered in:
  66. R. C. M. Consulting                       Object Oriented Analysis
  67. 2080 Cranbrook Rd.                        Object Oriented Design
  68. Green Oaks, Il 60048 (708) 918-1004       C++
  69.