Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!usc!orion.oac.uci.edu!uci-ics!rfg From: rfg@ics.uci.edu (Ronald Guilmette) Newsgroups: comp.lang.c++ Subject: Re: how do you define classes that refer to each other ??? Message-ID: <2605ECC2.24861@paris.ics.uci.edu> Date: 20 Mar 90 08:41:39 GMT References: <49689@wlbr.IMSD.CONTEL.COM> Reply-To: rfg@ics.uci.edu (Ronald Guilmette) Organization: UC Irvine Department of ICS Lines: 60 In article <49689@wlbr.IMSD.CONTEL.COM> mh@awds26.UUCP (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 ? Here's a simple example >to demonstrate my problem. > > > class thing { > int data[5]; > void execute(stack &s) { > s.push(this); > } > } > > class stack { > stack_store thing[500]; > int si; > private: > void push(thing *t); > stack() { > si = 0; > } > } > > void > stack::push(thing *t) { > stack_store[si++] = *t; > } > > main() { > thing mything; > stack mystack; > > mything.execute(); > } > >Now, the question is. How do I properly forward declare class 'stack' >to class 'thing' so that everybody is happy ? Declare: class stack; before the declaration of class "thing". Then move the body of the function thing::execute out of the "thing" class declaration and put it after the class declaration for class "stack". >I have the stroustrup C++ book as well as the Dewhurst/Stark C++ book >so an admonishing "you'll find this on page in Stroustrup you >moron" is fine :-). I've scanned through these two books but have not >found the answer. You'll find this on page in Stroustrup you moron. :-) (Sorry. I couldn't resist. :-) // Ron Guilmette (rfg@ics.uci.edu) // C++ Entomologist // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.