home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!math.fu-berlin.de!ira.uka.de!slsvaat!josef!kanze
- From: kanze@us-es.sel.de (James Kanze)
- Subject: Re: help with pass-by-reference
- In-Reply-To: hlf@nic.cerf.net's message of 23 Jan 1993 02:15:34 GMT
- Message-ID: <KANZE.93Jan28184436@slsvdnt.us-es.sel.de>
- Sender: news@us-es.sel.de
- Organization: SEL
- References: <1993Jan22.231848.3690@nosc.mil> <1jq9o6INNfee@news.cerf.net>
- Distribution: usa
- Date: 28 Jan 93 18:44:36
- Lines: 58
-
- In article <1jq9o6INNfee@news.cerf.net> hlf@nic.cerf.net (Howard
- Ferguson) writes:
-
- |> In article <1993Jan22.231848.3690@nosc.mil> atkins@nosc.mil (Hugh T. Atkins) writes:
- |> >Here's the setup, I have a class called Card and declare an array as
- |> >follows
- |> >
- |> >
- |> >Card myHand[5]; // this is an array of 5 playing cards
- |> >
- |> >I want to write a function which tests for pairs of cards and I want
- |> >to use pass-by-reference but I can't figure out how to declare the
- |> >function prototype.
- |> >
- |> >I'd like the function to return an int, something like this
- |> >
- |> >int hasPair( const Card& );
- |> >
- |> >
-
- |> >if( hasPair( ?????? ) )
- |> > pairs++;
- |> MAybe what you want is
- |> int hasPair ( const Card & *cardArray) {}
-
- |> Because you can not move up an down the array with a reference i.e
- |> references have no equivalent to pointer arithmatic.
-
- |> But now that you are only passing a pointer so you might as well just
- |> pass it by value. If you tried to write this as a reference to an
- |> array instead you would have the same effect because in C/C++ the
- |> name of an array is a pointer.
-
- |> The bottom line is we should pass composite objects/structures/
- |> aggregats/ any-thing-longer-than-a-couple-of-bytes as a reference,
- |> but on the occations where more flexibility is required then a
- |> pointer must be used. This is one of those cases.
- |> with
-
- Even better, why not declare a Hand object, thus:
-
- class Hand
- {
- public :
- Hand( StackOfCards& deck ) ;
- int hasPair() const ;
- private :
- Card* myCards[ 5 ] ;
- } ;
-
- The constructor could arrange to obtain the five Cards needed (maybe
- by calling the pick method in deck five times), and all of the member
- functions could access them as needed.
- --
- James Kanze email: kanze@us-es.sel.de
- GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
- Conseils en informatique industrielle --
- -- Beratung in industrieller Datenverarbeitung
-