home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 20104 < prev    next >
Encoding:
Text File  |  1993-01-29  |  2.4 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!math.fu-berlin.de!ira.uka.de!slsvaat!josef!kanze
  3. From: kanze@us-es.sel.de (James Kanze)
  4. Subject: Re: help with pass-by-reference
  5. In-Reply-To: hlf@nic.cerf.net's message of 23 Jan 1993 02:15:34 GMT
  6. Message-ID: <KANZE.93Jan28184436@slsvdnt.us-es.sel.de>
  7. Sender: news@us-es.sel.de
  8. Organization: SEL
  9. References: <1993Jan22.231848.3690@nosc.mil> <1jq9o6INNfee@news.cerf.net>
  10. Distribution: usa
  11. Date: 28 Jan 93 18:44:36
  12. Lines: 58
  13.  
  14. In article <1jq9o6INNfee@news.cerf.net> hlf@nic.cerf.net (Howard
  15. Ferguson) writes:
  16.  
  17. |> In article <1993Jan22.231848.3690@nosc.mil> atkins@nosc.mil (Hugh T. Atkins) writes:
  18. |> >Here's the setup, I have a class called Card and declare an array as
  19. |> >follows
  20. |> >
  21. |> >
  22. |> >Card myHand[5];  // this is an array of 5 playing cards
  23. |> >
  24. |> >I want to write a function which tests for pairs of cards and I want
  25. |> >to use pass-by-reference but I can't figure out how to declare the
  26. |> >function prototype.
  27. |> >
  28. |> >I'd like the function to return an int, something like this
  29. |> >
  30. |> >int hasPair( const Card& );
  31. |> >
  32. |> >
  33.  
  34. |> >if( hasPair( ?????? ) )
  35. |> >  pairs++;
  36. |> MAybe what you want is 
  37. |> int hasPair ( const Card & *cardArray) {}
  38.  
  39. |> Because you can not move up an down the array with a reference i.e
  40. |> references have no equivalent to pointer arithmatic.
  41.  
  42. |> But now that you are only passing a pointer so you might as well just
  43. |> pass it by value. If you tried to write this as a reference to an 
  44. |> array instead you would have the same effect because in C/C++ the 
  45. |> name of an array is a pointer.
  46.  
  47. |> The bottom line is we should pass composite objects/structures/
  48. |> aggregats/ any-thing-longer-than-a-couple-of-bytes as a reference,
  49. |> but on the occations where more flexibility is required then a
  50. |> pointer must be used. This is one of those cases.
  51. |> with
  52.  
  53. Even better, why not declare a Hand object, thus:
  54.  
  55.     class Hand
  56.     {
  57.     public :
  58.                 Hand( StackOfCards& deck ) ;
  59.         int        hasPair() const ;
  60.     private :
  61.         Card*        myCards[ 5 ] ;
  62.     } ;
  63.  
  64. The constructor could arrange to obtain the five Cards needed (maybe
  65. by calling the pick method in deck five times), and all of the member
  66. functions could access them as needed.
  67. --
  68. James Kanze                             email: kanze@us-es.sel.de
  69. GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
  70. Conseils en informatique industrielle --
  71.                    -- Beratung in industrieller Datenverarbeitung
  72.