home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!news.univie.ac.at!hp4at!rcvie!rcsw50!se_rossb
- From: se_rossb@rcvie.co.at (Bernhard Rossboth)
- Subject: Re: Multiple Header Files Problem
- Message-ID: <1992Nov19.153620.15831@rcvie.co.at>
- Sender: usenet@rcvie.co.at (Network News)
- Nntp-Posting-Host: rcsw50.rcvie.co.at
- Reply-To: se_rossb@rcvie.co.at
- Organization: Alcatel ELIN Research
- References: <1240@saxony.pa.reuter.COM>
- Date: Thu, 19 Nov 1992 15:36:20 GMT
- Lines: 53
-
-
- >In <5189@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
- >
- >>Problem is that a.hh gets #included, #includes b.hh, which tries to
- >>#include a.hh. This will fail, since b.hh will not see all declarations
- >>in a.hh (A_HH is already defined).
- >
-
- Maybe a solution to your dilemma is using inside of the header file for at least
- one of the two classes only pointers or references to objects of the other type.
- Doing so there is no need to include the header file (e.g. b.hh) inside the header
- file of the other class (a.hh), but only in a.cc you have to really include b.hh.
-
- An example will show you what I mean:
-
- ----------- a.hh -------------
- class B; // There is a class B somewhere around
-
- class A {
- B *bp; // Pointer to an existing class is fine, but:
- // B theB; would not work!! How much space needs a B???
- public:
- void useB(B&b); // Reference to an existing class is fine
- };
-
- ------------ end of a.hh --------
-
- ------------ b.hh --------------
- #include <a.hh> // must include parents definition!
-
- class B: public A {
- public:
- int i; // just some junk of no interest
- B() { i= 1;}
- };
-
- ------------ end of b.hh ---------
-
- ------------ a.cc ---------------
-
- #include <b.hh> // now I have to know all methodes and members of B
-
- void A::useB(B &b)
- {
- cout << b.i;
- }
- ------------ end of a.cc ---------
-
- Barny :-{)
-
- PS: Using pointers instead of static members and declaring the classes instead of
- including the header files may reduce the compile-time of your project significant!
-
-