home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19794 < prev    next >
Encoding:
Internet Message Format  |  1993-01-23  |  1.9 KB

  1. Path: sparky!uunet!usc!news.cerf.net!nic.cerf.net!hlf
  2. From: hlf@nic.cerf.net (Howard Ferguson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: help with pass-by-reference
  5. Date: 23 Jan 1993 02:15:34 GMT
  6. Organization: CERFnet Dial n' CERF Customer Group
  7. Lines: 47
  8. Distribution: usa
  9. Message-ID: <1jq9o6INNfee@news.cerf.net>
  10. References: <1993Jan22.231848.3690@nosc.mil>
  11. NNTP-Posting-Host: nic.cerf.net
  12. Keywords: c++,reference,pass
  13.  
  14. In article <1993Jan22.231848.3690@nosc.mil> atkins@nosc.mil (Hugh T. Atkins) writes:
  15. >Here's the setup, I have a class called Card and declare an array as
  16. >follows
  17. >
  18. >
  19. >Card myHand[5];  // this is an array of 5 playing cards
  20. >
  21. >I want to write a function which tests for pairs of cards and I want
  22. >to use pass-by-reference but I can't figure out how to declare the
  23. >function prototype.
  24. >
  25. >I'd like the function to return an int, something like this
  26. >
  27. >int hasPair( const Card& );
  28. >
  29. >
  30.  
  31. >if( hasPair( ?????? ) )
  32. >  pairs++;
  33. MAybe what you want is 
  34. int hasPair ( const Card & *cardArray) {}
  35.  
  36. Because you can not move up an down the array with a reference i.e
  37. references have no equivalent to pointer arithmatic.
  38.  
  39. But now that you are only passing a pointer so you might as well just
  40. pass it by value. If you tried to write this as a reference to an 
  41. array instead you would have the same effect because in C/C++ the 
  42. name of an array is a pointer.
  43.  
  44. The bottom line is we should pass composite objects/structures/
  45. aggregats/ any-thing-longer-than-a-couple-of-bytes as a reference,
  46. but on the occations where more flexibility is required then a
  47. pointer must be used. This is one of those cases.
  48. with
  49.  
  50. if ( hasPair(myHand[0]))
  51. but the first thing you are going to have to do with the card
  52. is take the address of it inside the function. It is much cleaner
  53. if the function takes a pointer and you can call it with
  54.  
  55. if (hasPair(myHand))
  56.  
  57.     hlf
  58. You could of course just pass a reference to the first object,
  59.  
  60.  
  61.