Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!casbah.acns.nwu.edu!nucsrl!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.lang.c++ Subject: Re: Message-ID: <1991Jun6.144347.13450@clear.com> Date: 6 Jun 91 14:43:47 GMT References: <1991May28.162856.12685@linus.mitre.org> Organization: Clear Communications, Inc. Lines: 68 In article <1991May28.162856.12685@linus.mitre.org> chaloux@maestro.mitre.org (Dave Chaloux) writes: ... Paraphrase... [Dave has base class for ports and a base class for messages and wants all derived ports and messages to support the following semantics:] > >message->write(&port_object) > [... stuff deleted...] >To set this up, class Port needs to know about Message_A class and Message_B >class. Message_A and Message_B class need to know about type Port. > >Now imagine you have your include files. The header for class Message_A includes >Port.h . The header for Port has to include Message_A.h . When compiling, one >of the class definitions is going to come first and the definition for the >other class is undefined. OUCH. > >WHERE AM I MISSING THE BOAT? > Dave: Try this: ---------------- message.h ---------------- class Port; // declaration of class port.. #include "port.h" not needed. class Message { ... void write(Port&); ... }; --------------- port.h ---------------------- class Message; // declaration again. #include "message.h" not needed class Port { ... void write(Message&); ... } ----------------- message.cc -------------- #include "message.h" #include "port.h" // message implementation needs to know port semantics. void Message::write(Port& thePort) { thePort.write(*this); } ------------------ port.cc ---------------- #include "port.h" void Port::write(Message& theMessage) { ... whatever ports do. } In general the following rule will help keep you out of trouble... If the specification of a class does not need to know the semantics of one of its constituents, then the constituent class should be declared, not #included. -- +-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for | | rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want | | uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all | +----------------------+:R::R::CCC:M:::::M:| the blame. So there. |