home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11695 < prev    next >
Encoding:
Internet Message Format  |  1992-07-28  |  2.7 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!cs.utexas.edu!sdd.hp.com!wupost!gumby!destroyer!ubc-cs!mala.bc.ca!epp
  2. From: epp@mala.bc.ca (Lorne Epp)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP with overloading binary operator...
  5. Message-ID: <1992Jul28.142121.606@mala.bc.ca>
  6. Date: 28 Jul 92 14:21:21 -0700
  7. References: <28JUL199210114546@ariel.lerc.nasa.gov>
  8. Organization: Malaspina College
  9. Lines: 74
  10.  
  11. In article <28JUL199210114546@ariel.lerc.nasa.gov>, smneyln@ariel.lerc.nasa.gov (Michael Neylon) writes:
  12. > I have the following class...
  13. > class ARRAYBASE {
  14. > private: float matrix[MAXSIZE][MAXSIZE];
  15. > public: int xsize, ysize;
  16. >     ARRAYBASE(int x, int y) {xsize=x+1;ysize=y+!;};
  17. > // put + get handlers
  18. > // overloaded operator= which is fine
  19. > // Inverse() function, taken from _Num. Rec. in C_
  20. >     ARRAYBASE ARRAYBASE::operator*(ARRAYBASE array1, ARRAYBASE array2)
  21. >     {
  22. >         //fill the array using matrix multiplication
  23. >         //fill in matrix[][] and xsize, and ysize vars
  24. >         return *this;
  25. >     };
  26. > };
  27. > (obviously, this defines arrays with a base as 1 (a[1..6][1..6]))
  28. > However, when i compile, i get...
  29. > ARRAYBASE::operator*() must take only 0 or 1 arguments
  30.  
  31. Since you have declared operator* as a member of arraybase, it has an
  32. implied argument, the <this> pointer, which is the left operand of the
  33. expression A2*A3.  This means that an overloaded binary operator that
  34. is implemented as a member function should have only one explicit
  35. argument.  There are two things you can do:
  36.  
  37.     //
  38.     // Member function - implied argument is ARRAYBASE *this
  39.     //
  40.      ARRAYBASE ARRAYBASE::operator*(const ARRAYBASE &array1 )
  41.      {
  42.         ARRAYBASE array;
  43.         /* Code to multiply *this by array1, putting
  44.             result in array */
  45.          return array;
  46.      };
  47.     
  48. or...
  49.     //
  50.     // Friend function - there is no implied argument, so both
  51.     // arguments appear in the parameter list.
  52.     //
  53.      friend 
  54.     ARRAYBASE ARRAYBASE::operator*(const ARRAYBASE &array1, 
  55.                        const ARRAYBASE &array2 )
  56.      {
  57.         ARRAYBASE array;
  58.         /* Code to multiply array1 by array2, putting
  59.             result in array */
  60.          return array;
  61.      };
  62.  
  63. (Note that the arguments are now declared "const ARRAYBASE &" instead
  64. of "ARRAYBASE" - this is not required, but it saves memory and
  65. copy constructor calls.)
  66.  
  67. > huh? this is the same format that I can find in several C++ books for
  68. > overloaded binary operators.  Whats wrong?!
  69. Your code resembles the format for overloaded binary operators with
  70. side effects, such as
  71.  
  72.     const FOO &FOO::operator+=( const FOO &foo )     // Only one argument
  73.     { /* add foo to *this leaving result in *this */
  74.       return *this;
  75.     }
  76. Use this form when you want side effects.  I don't think you want them
  77. in the example you gave, though.
  78.  
  79.  
  80. Lorne Epp
  81. epp@mala.bc.ca
  82.