home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!munnari.oz.au!goanna!djds
- From: djds@goanna.cs.rmit.oz.au (DARYL JOHN D'SOUZA)
- Newsgroups: comp.lang.c++
- Subject: operator overloading
- Keywords: pointer usage
- Message-ID: <14513@goanna.cs.rmit.oz.au>
- Date: 14 Sep 92 05:00:45 GMT
- Organization: Comp Sci, RMIT, Melbourne, Australia
- Lines: 46
-
- I am seeking clarification of the following note in the Stroustrup text
- (bottom of page 236):
-
- "Pointers can not be used because it is not possible to redefine
- the meaning of an operator when applied to a pointer."
-
- I tried the following program which seems to compile and run, in contradiction
- to this statement. I'm sure the problem lies with my misinterpretation of
- Stroustrup's note, but either way, would someone please elaborate. Here is
- my C++ program:
-
-
- //--------program begin
- #include <iostream.h>
- #include <iomanip.h>
-
- class nat { // natural number class
- int val;
- public:
- nat(int i) { val = i; }
- nat(const nat & n) { val = n.val; }
- nat & operator = (const nat & n) { val = n.val; return *this; }
- nat & operator = (int i) { val = i; return *this; }
- nat operator +(const nat * & r) { return nat(val + r->val); }
- int access() { return val; }
- };
-
- main()
- {
- nat i = 10, j = 20, *p = &j;
-
- i = i + p;
- cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
- i = *p + &i;
- cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
- *p = i + &j;
- cout << i.access() << ' ' << j.access() << ' ' << (*p).access() << endl;
- }
- //---------program end
-
- Thanks.
-
- Daryl D'Souza
- --
- Dept. of Computer Science, RMIT, GPO Box 2476V Melbourne 3000, Australia.
- email: djds@rmit.edu.au. Tel: +61 3 660 2927/2348; Fax: +61 3 662 1617.
-