Date: Thu, 6 Mar 1997 12:52:49 GMT Message-Id: <1.5.4.16.19970306125320.268fb65e@mailhost.sm.ic.ac.uk> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" To: an118 AT chebucto DOT ns DOT ca From: Paul Dixon

Subject: Re: HELP - need help resolving conundrum with classes Cc: djgpp AT delorie DOT com >How can I declare two different classes, where each class has a member >variable of the other class type. My problem is how to get the compiler to >recognize a variable type before it has been created. It isnt possible (AFAIK) to do exactly what you want - the definitions for 'sizeof' become recursive - but you can create classes each with member POINTERS to each other, as // This block in the header file (thing.h) // declare both classes class Owner; class Cat; // now define interface for each class class Owner; { Owner(); ~Owner(); int cats; Cat **cat; public: void Own( Cat * ); } class Cat { Cat(); ~Cat(); int owners; Owner **owner; public: void Owned( Owner * ); } // In the constructor for each, initialise empty lists Cat::Cat() { owners = 0; owner = NULL; } Owner::Owner() { cats = 0; cat = NULL; } // and provide mechanisms for assigning the links void Owner::Own( Cat *c ) { if (c) { // we ought to search the list and only add if not already present! if (!found) { cat = realloc( ++cats * sizeof( Cat * ) ); cat[cats-1] = c; c->Owned( this ); }; }; } etc etc You do need to be careful about creating the links in a symmetrical fashion Please note the code snippet above is based on code I use but rewritten heavily to fit your example problem and is nowhere near complete - it is only intended to demonstrate a technique.... Paul --------------------------------------------------------------------- Paul Dixon Software Engineer, Dept of Paediatrics Telephone: (+44) 171 725 6258 St Mary's Hospital Medical School Fax: (+44) 171 725 6284 Norfolk Place, London W2 1PG, UK Email: p DOT dixon AT ic DOT ac DOT uk A Constituent College of Imperial College of Science, Technology and Medicine ------------------------------------------------------------------------