Topic: ref. [ ] vs cref. [ ]
Author: Hyman Rosen <hymie@prolifics.com>
Date: 2000/04/18 Raw View
N J Chackowsky <chackowsky@BrandonSD.MB.CA> writes:
> In every implementation of a vector class I've seen there are always two
> versions of the subscripting operator. For instance, from the STL vector
> in DJGPP:
>
> reference operator[ ](size_type __n) { return *(begin() + __n); }
> and
> const_reference operator[ ](size_type __n) const { return *(begin() +
> __n); }
>
> It was explained to me that the compiler would select the first version
> in a situtation like a[5] = 9; where the contents of the vector are
> being changed, and the second version in a situation like b = a[0];
> where the contents are being examined only.
It was explained to you incorrectly. The first version will be chosen
when a is not const, and the second one will be chosen when a is const.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: 2000/04/18 Raw View
In article <38FB6E77.640ED129@brandonsd.mb.ca> nick@arcticmail.com writes:
>In every implementation of a vector class I've seen there are always two
>versions of the subscripting operator. For instance, from the STL vector
>in DJGPP:
>
>reference operator[ ](size_type __n) { return *(begin() + __n); }
> and
>const_reference operator[ ](size_type __n) const { return *(begin() +
>__n); }
>
>It was explained to me that the compiler would select the first version
>in a situtation like a[5] = 9; where the contents of the vector are
>being changed, and the second version in a situation like b = a[0];
>where the contents are being examined only.
>
>But I went ahead and stuck a cout << 'R'; in the first version (before
>the return), and a cout << 'C'; in the second version. No matter what I
>try to do in the client program, only the reference operator function is
>ever being called. (I get many Rs, no Cs at all!)
>
>Can someone help me to understand what's going on here? Or is this a
>bug/feature/inconsistency in DJGPP only?
You're kinda caught in a cause and effect relationship.
That is to say, it isn't that the second version is only picked
when you do something such as b = a[0];, but in that if a is const,
then that is the only thing you can do, as a[5] = 9; would be
an error. So, which it picks depends upon whether or not the
object being used is const or not. And then it may kick it
out as an error depending upon whether or not the object being used
is const or not. I'll say that again in C++'ese:
MyVector aa;
aa[5] = 9; // picks first, and aa can be assigned to
b = aa[0]; // picks first, and aa can be retrieved from
const MyVector a;
a[5] = 9; // picks first, but a cannot be assigned to
b = a[0]; // picks first, and a can be retrieved from
- Greg
--
Comeau Computing, Producers of Comeau C/C++ 4.2.42 (4.2.43 BETA starting)
Try Comeau C++ online at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: N J Chackowsky <chackowsky@BrandonSD.MB.CA>
Date: 2000/04/18 Raw View
In every implementation of a vector class I've seen there are always two
versions of the subscripting operator. For instance, from the STL vector
in DJGPP:
reference operator[ ](size_type __n) { return *(begin() + __n); }
and
const_reference operator[ ](size_type __n) const { return *(begin() +
__n); }
It was explained to me that the compiler would select the first version
in a situtation like a[5] = 9; where the contents of the vector are
being changed, and the second version in a situation like b = a[0];
where the contents are being examined only.
But I went ahead and stuck a cout << 'R'; in the first version (before
the return), and a cout << 'C'; in the second version. No matter what I
try to do in the client program, only the reference operator function is
ever being called. (I get many Rs, no Cs at all!)
Can someone help me to understand what's going on here? Or is this a
bug/feature/inconsistency in DJGPP only?
Nick.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Matt Austern <austern@sgi.com>
Date: 2000/04/18 Raw View
N J Chackowsky <chackowsky@BrandonSD.MB.CA> writes:
> In every implementation of a vector class I've seen there are always two
> versions of the subscripting operator. For instance, from the STL vector
> in DJGPP:
>
> reference operator[ ](size_type __n) { return *(begin() + __n); }
> and
> const_reference operator[ ](size_type __n) const { return *(begin() +
> __n); }
>
> It was explained to me that the compiler would select the first version
> in a situtation like a[5] = 9; where the contents of the vector are
> being changed, and the second version in a situation like b = a[0];
> where the contents are being examined only.
That explanation was wrong. The compiler selects the second version
if it's called for a const vector, and the first version otherwise.
Overload resolution is local; the compiler isn't supposed to examine
context. If the compiler sees the expression "a[0]", how is it
supposed to figure out whether or not the returned value is going to
be modified?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/04/18 Raw View
N J Chackowsky wrote:
> It was explained to me that the compiler would select the first version
> in a situtation like a[5] = 9; where the contents of the vector are
> being changed, and the second version in a situation like b = a[0];
> where the contents are being examined only.
Whoever explained it explained it wrong.
The const version of the operator is called when the vector itself is const.
The idea is that when [] is called on a vector, you get a modifiable reference
to the element. When [] is called on a const vector, you get a non-modifiable
reference.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]