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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!spool.mu.edu!sol.ctr.columbia.edu!ira.uka.de!ira.uka.de!slsvaat!us-es.sel.de!reindorf
  3. From: reindorf@us-es.sel.de (Charles Reindorf)
  4. Subject: Re: Multiple Header Files Problem
  5. Message-ID: <1992Nov10.104447.12882@us-es.sel.de>
  6. Sender: news@us-es.sel.de
  7. Organization: SEL-Alcatel Line Transmission Systems Dept. US/ES
  8. References: <720993360snx@trmphrst.demon.co.uk> <rmartin.721359424@thor> 
  9. Date: Tue, 10 Nov 92 10:44:47 GMT
  10. Lines: 74
  11.  
  12. In article <rmartin.721359424@thor>, rmartin@thor.Rational.COM (Bob Martin) writes:
  13. |> A more interesting variation on this issue is the problem of "circular
  14. |> inclusion".  Consider a source file: "a.h" which includes "b.h".
  15. |> However, "b.h" includes "a.h".  This trivial example will fail to
  16. |> compile.  Unless the compiler is very clever (I haven't seen one yet),
  17. |> it will sit and spin until it exhauses some resouce (usually memory),
  18. |> and then it will die a horrible death.  If it prints an error at all,
  19. |> the error is nearly certain to have nothing to do with the circular
  20. |> containment.
  21. |> 
  22. |> [ etc.]
  23.  
  24. It is usual, in header files, to structure them so :
  25.  
  26.  
  27. | # ifndef <HeaderFileName>_h
  28. | # define <HeaderFileName>_h
  29. | ... body of include file ...
  30. | # endif
  31.  
  32.  
  33. Where of course <HeaderFileName> is some valid identifier based on the name of
  34. the header file (and intended to be unique).
  35.  
  36. This prevents cyclic dependencies and has been in common practice for, well, ages.
  37.  
  38. N.B. It can give rise to other dependency problems, e.g.
  39.  
  40. Header File A.h:
  41.  
  42. |  # ifndef A_h
  43. |  # define A_h
  44. |  
  45. |  # include <B.h>
  46. |
  47. |  class A
  48. |  {  
  49. |     public:
  50. |      ... etc ...
  51. |     private:
  52. |     B *b;
  53. |     ... etc ...
  54. |  };
  55. |  
  56. |  # endif
  57.  
  58. Header File B.h:
  59.  
  60. |  # ifndef B_h
  61. |  # define B_h
  62. |
  63. |  # include <A.h>
  64. |
  65. |  class B
  66. |  { 
  67. |     public:
  68. |     A a_func();
  69. |     ... etc ...
  70. |  };
  71. |
  72. |  # endif
  73.  
  74. In which neither header file will scan, but then using forward declarations instead
  75. of direct includes generally solves these problems.
  76.  
  77. All options are my own etc.
  78.  
  79. --- Charles Reindorf
  80.  
  81.  
  82.