home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13611 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  2.3 KB

  1. Path: sparky!uunet!pmafire!news.dell.com!swrinde!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!wsu-cs!vela!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc10.cs.uiuc.edu!pjl
  2. From: pjl@sparc10.cs.uiuc.edu (Paul Lucas)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: operator overloading
  5. Keywords: pointer usage
  6. Message-ID: <1992Sep14.152926.5956@sunb10.cs.uiuc.edu>
  7. Date: 14 Sep 92 15:29:26 GMT
  8. References: <14513@goanna.cs.rmit.oz.au>
  9. Sender: news@sunb10.cs.uiuc.edu
  10. Organization: University of Illinois at Urbana-Champaign
  11. Lines: 57
  12.  
  13. In <14513@goanna.cs.rmit.oz.au> djds@goanna.cs.rmit.oz.au (DARYL JOHN D'SOUZA) writes:
  14.  
  15. >I am seeking clarification of the following note in the Stroustrup text
  16. >(bottom of page 236):
  17.  
  18. >    "Pointers can not be used because it is not possible to redefine 
  19. >    the meaning of an operator when applied to a pointer."
  20.  
  21. >I tried the following program which seems to compile and run, in contradiction
  22. >to this statement.  I'm sure the problem lies with my misinterpretation of
  23. >Stroustrup's note, but either way, would someone please elaborate.  Here is
  24. >my C++ program:
  25.  
  26.  
  27. >//--------program begin
  28. >#include <iostream.h>
  29. >#include <iomanip.h>
  30.  
  31. >class nat { // natural number class
  32. >    int val;
  33. >public:
  34. >    nat(int i) { val = i; }
  35. >    nat(const nat & n) { val = n.val; }
  36. >    nat & operator = (const nat & n) { val = n.val; return *this; }
  37. >    nat & operator = (int i) { val = i; return *this; }
  38. >    nat operator +(const nat * & r) { return nat(val + r->val); }
  39. >    int access() { return val; }
  40. >};
  41.  
  42. >main()
  43. >{
  44. >    nat i = 10, j = 20, *p = &j;
  45.  
  46. >    i = i + p;
  47. >    cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
  48. >    i = *p + &i;
  49. >    cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
  50. >    *p = i + &j;
  51. >    cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
  52. >}
  53.  
  54. *****>    So what's the problem?  The line:
  55.  
  56.         i = i + p;
  57.     
  58.     is:
  59.         i.operator=( i.operator+( p ) );
  60.     
  61.     that is, you've always got a 'nat' on the LHS of the '+'.  The
  62.     operator is being overloaded for that, not its argument.
  63.     Overloaded operators must have at least one class or
  64.     class-reference argument; for members, this argument is
  65.     implicit.
  66. -- 
  67.     - Paul J. Lucas                University of Illinois    
  68.       AT&T Bell Laboratories        at Urbana-Champaign
  69.       Naperville, IL            pjl@cs.uiuc.edu
  70.