Question regarding operator[]() and const

V

velthuijsen

I was trying out the binary operators
and did the following:

class mv
{
private:
int* Storage;
int StSize;
public:
mv(int Size = 0);
~mv();
mv(const mv& Right);
mv& operator=(const mv& Right);
int& operator[](int Position);
int Size() const;
};

mv operator+(const mv& Left, const mv& Right)
{
int ResultSize;
if (Left.Size() < Right.Size())
{
ResultSize = Left.Size();
}
else
{
ResultSize = Right.Size();
}
mv Result(ResultSize);
for (int i = 0; i < ResultSize; ++i)
{
Result = Left + Right;
}
return (Result);
}

Which resulted in a compaint that a non const function was being
called by a const object.

so I altered
int& operator[](int Position);
to
int& operator[](int Position) const;

But when I did this I expected the compiler to complain that I was
trying to alter Result while it was constant.
It didn't. Why not?
 
B

bartek

(e-mail address removed) (velthuijsen) wrote in
I was trying out the binary operators
and did the following:

class mv
{
private:
int* Storage;
int StSize;
public:
mv(int Size = 0);
~mv();
mv(const mv& Right);
mv& operator=(const mv& Right);
int& operator[](int Position);
int Size() const;
};

(...)

You should provide two operator[] overloads for const and non-const
objects.
 
J

John Harrison

Which resulted in a compaint that a non const function was being
called by a const object.

so I altered
int& operator[](int Position);
to
int& operator[](int Position) const;

But when I did this I expected the compiler to complain that I was
trying to alter Result while it was constant.
It didn't. Why not?


You cannot return a non-const reference to a member from a const method. If
your code was allowed then this would compile

const mv x;
x[0] = 1; // modifying const object

Which I'm sure you agree makes no sense at all.

The answer is to have *two* operator[] defined, one const and one non-const.
Like this

int& operator[](int Position); // your original non-const

int operator[](int Position) const; // new const version

john
 
R

Rob Williscroft

velthuijsen wrote in in comp.lang.c++:
I was trying out the binary operators
and did the following:

class mv
{
private:
int* Storage;
int StSize;
public:
mv(int Size = 0);
~mv();
mv(const mv& Right);
mv& operator=(const mv& Right);
int& operator[](int Position);
int Size() const;
};

mv operator+(const mv& Left, const mv& Right)
{
int ResultSize;
if (Left.Size() < Right.Size())
{
ResultSize = Left.Size();
}
else
{
ResultSize = Right.Size();
}
mv Result(ResultSize);
for (int i = 0; i < ResultSize; ++i)
{
Result = Left + Right;
}
return (Result);
}

Which resulted in a compaint that a non const function was being
called by a const object.

so I altered
int& operator[](int Position);
to
int& operator[](int Position) const;

But when I did this I expected the compiler to complain that I was
trying to alter Result while it was constant.
It didn't. Why not?


Because you didn't ask it too, try:

int const & operator [] ( int Position ) const;

Note that you can have *both* const and non-const vertion's
of this member function in "mv", if you find that appropriate.

HTH.

Rob.
 
V

velthuijsen

You cannot return a non-const reference to a member from a const method. If
your code was allowed then this would compile

const mv x;
x[0] = 1; // modifying const object

Which I'm sure you agree makes no sense at all.

Yes that is why I asked what was wrong.
The answer is to have *two* operator[] defined, one const and one non-const.
Like this

int& operator[](int Position); // your original non-const

int operator[](int Position) const; // new const version

john

Neither one would complain. I was expecting that the second one would
complain if I would have tried result = i.
But as Willcroft pointed out I should have made it
int& const operator[](int Position) const;

The problem I had was that I hadn't realised that by doing result =
i I was modifying the reference returned, not the operator[] function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top