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

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Subject: Re: Multiple Header Files Problem
  5. Reply-To: nikki@trmphrst.demon.co.uk
  6. Distribution: world
  7. X-Mailer: cppnews $Revision: 1.20 $
  8. Organization: Trumphurst Ltd.
  9. Lines: 42
  10. Date: Thu, 5 Nov 1992 12:56:00 +0000
  11. Message-ID: <720993360snx@trmphrst.demon.co.uk>
  12. Sender: usenet@gate.demon.co.uk
  13.  
  14. In article <1992Nov03.133949.21465@rs6000.bham.ac.uk> pickerig@eee.bham.ac.uk (Mr. G. Pickering) writes:
  15. > Koh Thong Hwee (gthkoh@ntuix.ntu.ac.sg) wrote:
  16. > : The above class declarations gave problem as each declaration cannot be 
  17. > : completed because of the other.  Before class A is declared, it takes a
  18. > : pointer to class B.  But class B cannot be declared because it takes a
  19. > : pointer to class A (I have need to know its parent object).  There must
  20. > : be an obvious way out of this, however I just cannot see it...
  21. > : 
  22. > Rather than putting just "A*" and "B*" use "class A*" and "class B*"
  23. > instead. This tells the compiler that A and B are classes and it
  24. > resolves them later.
  25. There are compilers that won't recognise this syntax correctly. A more 
  26. portable solution is to make a separate forward declaration of class B 
  27. before declaring class A, thus ...
  28.  
  29. class B;        // Forward declare class B
  30.  
  31. class A
  32. {
  33. public
  34.    ...
  35.    B* pB;
  36.    void foo();
  37. };
  38.  
  39. void A::foo()
  40. {   
  41.    pB = new B(this);
  42. }
  43.  
  44. class B
  45. {
  46. public
  47.    ...
  48.    A* pParentA;
  49.    inline B(A* pA) {pParentA = pA};
  50. };
  51.  
  52. -- 
  53. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  54. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  55.