home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c++
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!ux1.cso.uiuc.edu!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc1.cs.uiuc.edu!pjl
- From: pjl@sparc1.cs.uiuc.edu (Paul Lucas)
- Subject: Re: How do I... (regarding overloading)
- Message-ID: <1992Jul21.232959.13540@sunb10.cs.uiuc.edu>
- Sender: news@sunb10.cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- References: <1992Jul21.144856.583@ualr.edu>
- Distribution: usa
- Date: Tue, 21 Jul 1992 23:29:59 GMT
- Lines: 38
-
- In <1992Jul21.144856.583@ualr.edu> choate@acs.harding.edu (Brad S. Choate) writes:
-
- *****> For starters, this type of question belongs in comp.lang.c++.
-
- >Ok... I'm new at this business, so this should be easy for some of the experts
- >out there.
-
- >I know about operator overloading and I can implement it to some degree without
- >consulting the manuals too much, but I'm stuck on this one:
-
- > a[1]=x;
-
- >Ok. "a" is an object that I have created and I've overloaded the [] operator
- >to allow me to return a value at position "1" (in this case) from my object.
- >However, in this case, I'm not needing output, I'm trying to _assign_ a value
- >to that position. I know how to overload the "=" operator, but _how_ do you
- >do _both_ operators at the same time?
-
- *****> operator= has got nothing to do with it unless the thing that []
- returns is itself a class object (in which case operator= would
- be overloaded in _that_ class--whether operator= is overloaded
- in _this_ class is irrelevant...unless you're doing something
- non-canonical). operator[] should return a reference to the
- thing...that's it; the assignment just happens.
-
- class IntVect {
- int *v;
- public:
- // ...
- int &operator[]( int i ) { return v[i]; }
- };
-
- This is a naive example in that it doesn't do bounds-checking nor
- automatic "growing."
- --
- - Paul J. Lucas University of Illinois
- AT&T Bell Laboratories at Urbana-Champaign
- Naperville, IL pjl@cs.uiuc.edu
-