Topic: operator[] and const
Author: eball12@ursa.calvin.edu (Edward Ball)
Date: Fri, 4 Mar 1994 02:03:08 GMT Raw View
My basic question: What is the proper way to declare the operator[] in a
class? (My compiler of choice is Turbo C++.) My first inclination is this:
class Array {
int * ptr;
public:
Array();
~Array();
unsigned size();
int & operator[](unsigned loc); // <------ look here
};
However, this will cause a warning (actually an error, but is processed as a
warning "to allow existing code to compile") in this function:
void output(const Array & array)
{
for (unsigned i=0; i<array.size(); i++)
cout << array[i] << endl;
}
because the compiler doesn't know that operator[] won't change array, though
it is obvious to us that it won't...
So, how about:
...
int & operator[](unsigned loc) const;
...
I have seen it done this way in one of the C++ books I have read, however,
my compiler doesn't even allow this declaration, which makes at least a little
sense -- returning a reference would allow the Array instance to change, which
violates the fact that it is a 'const' member function...
And, of course, there is:
...
const int & operator[](unsigned loc) const;
...
which works great, except that you can't do assignments like:
array[2] = 3;
My solution has been to make two operator[] functions, like this:
int & operator[](unsigned loc);
const int & operator[](unsigned loc) const;
Apparently 'const' is part of C++ name mangling, as the compiler treats these
as two separate functions and applies the 'const' one when it is necessary
to do so, as in the example program above, and applies the non-'const' one
for array[x] assignments, etc. This solution also compiles just fine using
the g++ compiler.
Is this the right way? If so, why haven't I seen it in any books? If not,
what is the right way? Thanks for your input...
--
///////////////////////////////////////////////////////////////////////////
// Edward Ball, .sigless Knight eball12@calvin.edu //
///////////////////////////////////////////////////////////////////////////
Author: bonnet@dutiak.twi.tudelft.nl (J. Bonnet)
Date: Fri, 4 Mar 1994 09:45:27 GMT Raw View
eball12@ursa.calvin.edu (Edward Ball) writes:
>My basic question: What is the proper way to declare the operator[] in a
>class? (My compiler of choice is Turbo C++.) My first inclination is this:
>class Array {
> int * ptr;
> public:
> Array();
> ~Array();
> unsigned size();
> int & operator[](unsigned loc); // <------ look here
> };
>However, this will cause a warning (actually an error, but is processed as a
>warning "to allow existing code to compile") in this function:
>void output(const Array & array)
>{
> for (unsigned i=0; i<array.size(); i++)
> cout << array[i] << endl;
>}
>because the compiler doesn't know that operator[] won't change array, though
>it is obvious to us that it won't...
>So, how about:
>...
>int & operator[](unsigned loc) const;
>...
>I have seen it done this way in one of the C++ books I have read, however,
>my compiler doesn't even allow this declaration, which makes at least a little
>sense -- returning a reference would allow the Array instance to change, which
>violates the fact that it is a 'const' member function...
>And, of course, there is:
>...
>const int & operator[](unsigned loc) const;
>...
>which works great, except that you can't do assignments like:
> array[2] = 3;
>My solution has been to make two operator[] functions, like this:
>int & operator[](unsigned loc);
>const int & operator[](unsigned loc) const;
>Apparently 'const' is part of C++ name mangling, as the compiler treats these
>as two separate functions and applies the 'const' one when it is necessary
>to do so, as in the example program above, and applies the non-'const' one
>for array[x] assignments, etc. This solution also compiles just fine using
>the g++ compiler.
>Is this the right way? If so, why haven't I seen it in any books? If not,
>what is the right way? Thanks for your input...
This is probably a legal way to do this. I am quite sure that Stroustrup
himself wrote something very similar in "The C++ programming language",
second edition. That should mean we can all get away with it...
==============================================================================
J. Bonnet
tel. (+31) 79 211492 email: bonnet@dutiag.twi.tudelft.nl
==============================================================================
"Time you enjoy wasting is no wasted time." (T.S. Elliot)