home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!news.cerf.net!nic.cerf.net!hlf
- From: hlf@nic.cerf.net (Howard Ferguson)
- Newsgroups: comp.lang.c++
- Subject: Re: help with pass-by-reference
- Date: 23 Jan 1993 02:15:34 GMT
- Organization: CERFnet Dial n' CERF Customer Group
- Lines: 47
- Distribution: usa
- Message-ID: <1jq9o6INNfee@news.cerf.net>
- References: <1993Jan22.231848.3690@nosc.mil>
- NNTP-Posting-Host: nic.cerf.net
- Keywords: c++,reference,pass
-
- 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
-
- if ( hasPair(myHand[0]))
- but the first thing you are going to have to do with the card
- is take the address of it inside the function. It is much cleaner
- if the function takes a pointer and you can call it with
-
- if (hasPair(myHand))
-
- hlf
- You could of course just pass a reference to the first object,
-
-
-