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?
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?