Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overloading based on const

44 views
Skip to first unread message

Paul

unread,
Nov 13, 2015, 8:20:46 AM11/13/15
to
Below is a standard textbook example of overloading the [] operator based on const.
However, it seems to me that both functions act as const member functions.
The function with signature object& operator[](int index) seems (to me) that it should also be const. After all, it doesn't change any member objects.

However, we get a compile error when we make both of these functions const (i.e by writing object& operator[](int index) const)

Why can't we make both of these functions const member functions -- since both of them don't change any member objects? The call would still be unambiguous since the version would depend on the constness of the called object.

Thank you for your help.

Paul


class object{
public:

object& operator[](int index)
{
return objArray[index];
}

const object& operator[](int index) const
{
return objArray[index];
}

private:
object* objArray;
};

Alf P. Steinbach

unread,
Nov 13, 2015, 8:40:42 AM11/13/15
to
On 11/13/2015 2:20 PM, Paul wrote:
>
> [snip]
> However, we get a compile error when we make both of these
> functions const (i.e by writing object& operator[](int index)
> const)
>
> Why can't we make both of these functions const member
> functions -- since both of them don't change any member
> objects?

The compiler has already told you why. Just read its error message.

Cheers & hth.,

- Alf

asetof...@gmail.com

unread,
Nov 13, 2015, 8:42:58 AM11/13/15
to
For me, not for C++:
Const is *not* to refer to a variable
but has to refer of memory
that variable occupy
So references and names
are constant if refer memory
one can read only, and can not
write.

Wouter van Ooijen

unread,
Nov 13, 2015, 8:45:59 AM11/13/15
to
Op 13-Nov-15 om 2:20 PM schreef Paul:
The compiler is a bit more clever than just looking at the code: the
first version can potentially be used by a *caller* to change the object:

a[ i ] = b;

More formally, in the first version, you are (in the return statement)
trying to convert a const object to a non-const reference, which is not
allowed. It is as if your code reads

const int i = 5;
int &r = i; // error!

Wouter


Victor Bazarov

unread,
Nov 13, 2015, 8:47:45 AM11/13/15
to
Did you intend class 'object' to contain a pointer to an array of data
of the same type? Or is it merely for the sake of example?

> };
>

While neither operator changes the contents of the object itself (the
pointer stays as it is), the logic suggests that the contents of memory
pointed to by the member (objArray) for an object declared 'const' are
not supposed to change either since they are *perceived* as the
"contents" (provided that the memory is somehow maintained by the object
itself and thus the *full responsibility* of the object).

To prevent the contents of the memory to which 'objArray' member points,
from being deliberately changed, the 'const' member function returns a
reference to const, while a non-const member function returns a
reference to a non-const object.

V
--
I do not respond to top-posted replies, please don't ask

Mr Flibble

unread,
Nov 13, 2015, 1:10:16 PM11/13/15
to
C++ supports both physical constness and logical constness and I think
you are claiming that you should not use logical constness in C++ which
is of course total nonsense.

Why support logical constness? Because it allows you to define an
interface that allows or disallows object mutation by different clients.
It ensures that an object will not change underfoot when performing
some sequence of steps and arguably can form part of a class's invariant.

I use logical constness a lot more than physical constness in C++ which
I suspect is the same for most C++ professionals.

/Flibble


Paul N

unread,
Nov 16, 2015, 6:09:21 PM11/16/15
to
The C++ FAQ explains this quite well, which makes me suspect you aren't aware of it. It's at http://www.parashift.com/c++-faq/ and while your immediate question is easy to find, I'd recommend reading much more of it.
0 new messages