From: "Jesper de Jong" Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.os.msdos.djgpp Subject: Re: Using one copy of class for many other classes Date: 27 Jun 1997 21:55:29 -0400 Organization: XS4ALL, networking for the masses Lines: 71 Sender: cppmods AT netlab DOT cs DOT rpi DOT edu Approved: dietmar DOT kuehl AT uni-konstanz DOT de Message-ID: <5p1quh$n9n@netlab.cs.rpi.edu> References: <5p0epd$gtj AT netlab DOT cs DOT rpi DOT edu> NNTP-Posting-Host: horn.informatik.uni-konstanz.de To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk scochran AT shoalsnet DOT com schreef in artikel <5p0epd$gtj AT netlab DOT cs DOT rpi DOT edu>... > In C++ I've run into yet another brick wall. I have a class called arena > that is a 50x50 arena for obstacles and robots (for now anyway). I'm > running into problems because I only want to have one copy of the arena and > multiple copies of the robots or whatever I want to put. > > Would I do it something like this? Have you ever heard of pointers?? Understanding pointers is *essential* when you want to program in C or C++. > int robot::robot(arena arenacopy) Constructors do not have return values (not even void), so the "int" here is illegal. If each robot needs a pointer top the arena it's in, just add a pointer to the arena member variable in class robot: class robot { private: arena * pArena; public: robot(arena * ptr); int robofunction(); // the rest of robot here... }; robot::robot(arena * ptr) : pArena(ptr) { } If you don't understand all this, you need to learn some more C++. Study some good books or take a course. > int robot::robofunction(void) > { > arenacopy.arenafunction(); > } int robot::robofunction() { pArena->arenafunction(); } > Would this work? > > Also I have an enum to define the objects in a buffer of the arena would it > be best to derive a class from arena and put all the enum and enum specific > (the ones that use the enums) functions there? Take a C++ course and reconsider your design. > > Micah Cochran > scochran AT shoalsnet DOT com Jesper -- NOTE: My return address is not my e-mail address to avoid automatic spam e-mail. Spammers are invited to send mail to postmaster AT localhost. My address is jesper (at) xs4all.nl. [ Send an empty e-mail to c++-help AT netlab DOT cs DOT rpi DOT edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]