home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- From: nikki@trmphrst.demon.co.uk (Nikki Locke)
- Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
- Subject: Re: Multiple Header Files Problem
- Reply-To: nikki@trmphrst.demon.co.uk
- Distribution: world
- X-Mailer: cppnews $Revision: 1.20 $
- Organization: Trumphurst Ltd.
- Lines: 42
- Date: Thu, 5 Nov 1992 12:56:00 +0000
- Message-ID: <720993360snx@trmphrst.demon.co.uk>
- Sender: usenet@gate.demon.co.uk
-
- In article <1992Nov03.133949.21465@rs6000.bham.ac.uk> pickerig@eee.bham.ac.uk (Mr. G. Pickering) writes:
- > Koh Thong Hwee (gthkoh@ntuix.ntu.ac.sg) wrote:
- > : The above class declarations gave problem as each declaration cannot be
- > : completed because of the other. Before class A is declared, it takes a
- > : pointer to class B. But class B cannot be declared because it takes a
- > : pointer to class A (I have need to know its parent object). There must
- > : be an obvious way out of this, however I just cannot see it...
- > :
- >
- > Rather than putting just "A*" and "B*" use "class A*" and "class B*"
- > instead. This tells the compiler that A and B are classes and it
- > resolves them later.
- There are compilers that won't recognise this syntax correctly. A more
- portable solution is to make a separate forward declaration of class B
- before declaring class A, thus ...
-
- class B; // Forward declare class B
-
- class A
- {
- public
- ...
- B* pB;
- void foo();
- };
-
- void A::foo()
- {
- pB = new B(this);
- }
-
- class B
- {
- public
- ...
- A* pParentA;
- inline B(A* pA) {pParentA = pA};
- };
-
- --
- Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
- trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
-