Thread View: comp.std.c++
4 messages
4 total messages
Started by Brad S. Choate
Tue, 21 Jul 1992 19:48
How do I... (regarding overloading)
Author: Brad S. Choate
Date: Tue, 21 Jul 1992 19:48
Date: Tue, 21 Jul 1992 19:48
25 lines
1446 bytes
1446 bytes
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?
Perplexed++
(any ideas?)
e-mail me if you have an answer! This one's been bugging me.
- Brad
--
Disclaimer: "I really don't care."
|) |) /\ |\ | Brad S. Choate | 1314 E. Market, Apt. 6
|) |\ || |/ | | Searcy, AR 72143
--------_-- | choate@acs.harding.edu | (501) 279-2584
<||()/\|[ | V.P., Software Extensions, Inc.
Re: How do I... (regarding overloading)
Author: Paul Lucas
Date: Tue, 21 Jul 1992 23:29
Date: Tue, 21 Jul 1992 23:29
38 lines
2011 bytes
2011 bytes
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
Re: How do I... (regarding overloading)
Author: Jim Adcock
Date: Thu, 23 Jul 1992 01:21
Date: Thu, 23 Jul 1992 01:21
22 lines
1412 bytes
1412 bytes
In article <1992Jul21.232959.13540@sunb10.cs.uiuc.edu> pjl@sparc1.cs.uiuc.edu (Paul Lucas) writes:
|*****> 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.
The overloading of operator= becomes relevant IF some committee members
succeed in changing the language such that functions cannot be invoked
on temporaries. Then the common approach of returning a SmartRef from
operator[] fails:
bitarray[100] = TRUE; // code in error if functions cannot
// be invoked on temporaries!
because this really means:
bitarray.operator[](100).operator=(TRUE);
where operator[] is returning a temporary SmartBitRef that in turn has
its operator= on it.
Re: How do I... (regarding overloading)
Author: Paul Lucas
Date: Thu, 23 Jul 1992 15:49
Date: Thu, 23 Jul 1992 15:49
21 lines
1689 bytes
1689 bytes
In <1992Jul23.012111.16784@microsoft.com> jimad@microsoft.com (Jim Adcock) writes: >In article <1992Jul21.232959.13540@sunb10.cs.uiuc.edu> pjl@sparc1.cs.uiuc.edu (Paul Lucas) writes: >|*****> 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. >The overloading of operator= becomes relevant IF some committee members >succeed in changing the language such that functions cannot be invoked >on temporaries. Why do they want to do that? If I want to call functions on temporaries, that's my business (and I accept any consequences). I think that compiler warnings are sufficient. -- - Paul J. Lucas University of Illinois AT&T Bell Laboratories at Urbana-Champaign Naperville, IL pjl@cs.uiuc.edu
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads