Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!ctrsol!emory!att!cbnewsl!dog From: dog@cbnewsl.ATT.COM (edward.n.schiebel) Newsgroups: comp.lang.c++ Subject: Re: A solution to the multiple inclusion problem Message-ID: <2466@cbnewsl.ATT.COM> Date: 27 Oct 89 11:09:51 GMT References: <1030@ncratl2.Atlanta.NCR.COM> Distribution: comp.lang.c++ Organization: AT&T Bell Laboratories Lines: 32 From article <1030@ncratl2.Atlanta.NCR.COM>, by dspoon@ncratl2.Atlanta.NCR.COM (Dave Witherspoon): > [...stuff deleted] > 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): > > A.hpp B.hpp > ----- ----- > #ifndef D_A #ifndef D_B > #define D_A #define D_B > #include "B.hpp" #include "A.hpp" > > class A { B* bp;} class B {A* ap;} > #endif #endif > Since class A only has a B*, and not a B you don't need to #include B.hpp but only tell the compiler B is a class. Thus: #ifndef D_A #ifndef D_B #define D_A #define D_B class B; class A; // DON'T NEED INCLUDE! class A { B* bp;} class B {A* ap;} #endif #endif Now, if A included an instance of a B and visa-versa, well, then you have problems. Ed Schiebel AT&T Bell Laboratories dog@vilya.att.com 201-386-3416