home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16562 < prev    next >
Encoding:
Text File  |  1992-11-19  |  2.0 KB  |  67 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.univie.ac.at!hp4at!rcvie!rcsw50!se_rossb
  3. From: se_rossb@rcvie.co.at (Bernhard Rossboth)
  4. Subject: Re: Multiple Header Files Problem
  5. Message-ID: <1992Nov19.153620.15831@rcvie.co.at>
  6. Sender: usenet@rcvie.co.at (Network News)
  7. Nntp-Posting-Host: rcsw50.rcvie.co.at
  8. Reply-To: se_rossb@rcvie.co.at
  9. Organization: Alcatel ELIN Research
  10. References: <1240@saxony.pa.reuter.COM>
  11. Date: Thu, 19 Nov 1992 15:36:20 GMT
  12. Lines: 53
  13.  
  14.  
  15. >In <5189@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  16. >
  17. >>Problem is that a.hh gets #included, #includes b.hh, which tries to
  18. >>#include a.hh.  This will fail, since b.hh will not see all declarations
  19. >>in a.hh (A_HH is already defined).
  20. >
  21.  
  22. Maybe a solution to your dilemma is using inside of the header file for at least
  23. one of the two classes only pointers or references to objects of the other type.
  24. Doing so there is no need to include the header file (e.g. b.hh) inside the header 
  25. file of the other class (a.hh), but only in a.cc you have to really include b.hh. 
  26.  
  27. An example will show you what I mean:
  28.  
  29. -----------   a.hh -------------
  30. class B;                        // There is a class B somewhere around 
  31.  
  32. class A {
  33.         B *bp;          // Pointer to an existing class is fine, but:
  34. //         B theB;        would not work!! How much space needs a B???
  35.     public:
  36.         void useB(B&b); // Reference to an existing class is fine
  37. };
  38.  
  39. ------------ end of a.hh --------
  40.  
  41. ------------  b.hh --------------
  42. #include <a.hh>            // must include parents definition!
  43.  
  44. class B: public A {
  45.     public:
  46.         int i;        // just some junk of no interest
  47.         B() { i= 1;}
  48. };
  49.  
  50. ------------ end of b.hh ---------
  51.  
  52. ------------  a.cc ---------------
  53.  
  54. #include <b.hh>                 // now I have to know all methodes and members of B
  55.  
  56. void A::useB(B &b)
  57. {
  58.     cout << b.i;
  59. }
  60. ------------ end of a.cc ---------
  61.  
  62.                             Barny :-{)
  63.  
  64. PS: Using pointers instead of static members and declaring the classes instead of
  65. including the header files may reduce the compile-time of your project significant!
  66.  
  67.