Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!mcsun!ukc!dcl-cs!aber-cs!odin!pcg From: pcg@odin.cs.aber.ac.uk (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: how do you define classes that refer to each other ??? Message-ID: Date: 23 Mar 90 11:25:07 GMT References: <49689@wlbr.IMSD.CONTEL.COM> Sender: pcg@aber-cs.UUCP Organization: Coleg Prifysgol Cymru Lines: 60 In-reply-to: mh@awds26.imsd.contel.com's message of 20 Mar 90 00:04:01 GMT In article <49689@wlbr.IMSD.CONTEL.COM> mh@awds26.imsd.contel.com (Mike Hoegeman) writes: This is probably a stupid question, but here goes anyway. how do you set up two classes that refer to each other ? Another heathen that does not hold my excessively precious advice as sacred :-)! If you write all your member functions *after* the class definition, a lot of problem disappear. You may also need to forward declare the classes, of course (if you use the typedef style names). The general scheme is: struct c1; /* forward declarations */ struct c2; struct ...; struct c1 { int m1; c2 *m2; int p1(int); friend f1(c2 &,char); ... }; struct c2 { double m1; c1 m2; double p1(double); friend f2(c2 &,short); ... }; ... int c1::p1(int i) { ... } int f1(c2 &o, char c) { ... } double c2::p1(double d) { ... } int f2(c2 &o,short s) { ... } ... Notice that of course you *cannot* have two classes that inherit from each other or that appear as fields in each other; true recursive definitions are not allowed. -- Piercarlo "Peter" Grandi | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcvax!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk