home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13595 < prev    next >
Encoding:
Text File  |  1992-09-13  |  1.6 KB  |  57 lines

  1. Path: sparky!uunet!munnari.oz.au!goanna!djds
  2. From: djds@goanna.cs.rmit.oz.au (DARYL JOHN D'SOUZA)
  3. Newsgroups: comp.lang.c++
  4. Subject: operator overloading
  5. Keywords: pointer usage
  6. Message-ID: <14513@goanna.cs.rmit.oz.au>
  7. Date: 14 Sep 92 05:00:45 GMT
  8. Organization: Comp Sci, RMIT, Melbourne, Australia
  9. Lines: 46
  10.  
  11. I am seeking clarification of the following note in the Stroustrup text
  12. (bottom of page 236):
  13.  
  14.     "Pointers can not be used because it is not possible to redefine 
  15.     the meaning of an operator when applied to a pointer."
  16.  
  17. I tried the following program which seems to compile and run, in contradiction
  18. to this statement.  I'm sure the problem lies with my misinterpretation of
  19. Stroustrup's note, but either way, would someone please elaborate.  Here is
  20. my C++ program:
  21.  
  22.  
  23. //--------program begin
  24. #include <iostream.h>
  25. #include <iomanip.h>
  26.  
  27. class nat { // natural number class
  28.     int val;
  29. public:
  30.     nat(int i) { val = i; }
  31.     nat(const nat & n) { val = n.val; }
  32.     nat & operator = (const nat & n) { val = n.val; return *this; }
  33.     nat & operator = (int i) { val = i; return *this; }
  34.     nat operator +(const nat * & r) { return nat(val + r->val); }
  35.     int access() { return val; }
  36. };
  37.  
  38. main()
  39. {
  40.     nat i = 10, j = 20, *p = &j;
  41.  
  42.     i = i + p;
  43.     cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
  44.     i = *p + &i;
  45.     cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
  46.     *p = i + &j;
  47.     cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
  48. }
  49. //---------program end
  50.  
  51. Thanks.
  52.  
  53. Daryl D'Souza
  54. -- 
  55. Dept. of Computer Science, RMIT, GPO Box 2476V Melbourne 3000, Australia. 
  56. email: djds@rmit.edu.au. Tel: +61 3 660 2927/2348; Fax: +61 3 662 1617.
  57.