Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!usc!apple!sun-barr!lll-winken!arisia!sgi!shinobu!odin!delrey!shap From: shap@delrey.sgi.com (Jonathan Shapiro) Newsgroups: comp.lang.c++ Subject: Re: A solution to the multiple inclusion problem Message-ID: <1144@odin.SGI.COM> Date: 27 Oct 89 17:31:48 GMT References: <14240@well.UUCP> <1989Oct23.191634.6345@cs.rochester.edu> <950@dutrun.UUCP> <1030@ncratl2.Atlanta.NCR.COM> Sender: news@odin.SGI.COM Distribution: comp Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 31 In article <1030@ncratl2.Atlanta.NCR.COM> dspoon@ncratl2.Atlanta.NCR.COM (Dave Witherspoon) writes: > >Say I have 2 classes, A and B. A contains a B*, and B contains an A*. >Thus, we have a circular dependency! One proposal (that I've tried): > The following should do what you want. The trick is to recognize that you don't really have a circular dependency at all: A.hpp B.hpp ----- ----- #ifndef A_HPP #ifndef B_HPP #define A_HPP #define B_HPP class B; class A; class A { B* bp; }; class B {A* ap;}; All C++ needs to know is that A/B are classes to construct a pointer to them. It doesn't need to know what the contents are until you go to use them. The only disadvantage to this approach is that A's inline member functions won't get inlined into the B inline member functions that are defined in the header, and vice versa. This sort of problem is tricky. If you try to do what you were doing, it is very easy for a 60 line implementation file to haul in 6000 lines wirth of headers. I've seen it happen.