1. @laserlight -
I think I didn't get some important things you described:
What do you mean by copy assignment?
You have also said that even if the const version is called in this assignment:
Code:
`m1(0, 0) = m2(0, 0);`
The return value of m2(0, 0) is actually a copy of this element in the matrix - so what's the meaning of returning a const reference here?

As for your last answer - it's right, but here, in our particular example, we didn't do any assignments/comparisons between objects but between ints...

Thank you!!

2. Originally Posted by HelpMeC
What do you mean by copy assignment?
Basically, if an object of some type already exists, and you're assigning to it another object of the same type such that it becomes a copy of that other object, then what you have is copy assignment. For int objects, this could look like:
Code:
```int x = 100;
int y = 123;
x = y; // copy assignment```
Originally Posted by HelpMeC
The return value of m2(0, 0) is actually a copy of this element in the matrix
No, I wrote that "the return value of m2(0, 0) will be copy assigned to the object referred to by the return value of m1(0, 0)".

Look back at my example above. This:
Code:
`m1(0, 0) = m2(0, 0);`
is equivalent to:
Code:
`x = y;`
but only because m1(0, 0) returns a non-const reference so that the element at 0, 0 in the matrix can be copy assigned to, and m2(0, 0) returns either a copy, a non-const reference, or a const reference so that it can be copied from.

(Well, if you return a copy, then what you'll actually have is move assignment after the copy construction rather than copy assignment, but for ints, move assignment is exactly the same thing as copy assignment.)

3. wow.. don't understand anything

4. So, you don't understand what is copy assignment?

5. @laserlight -
First, I don't know what move assignment is actually - we haven't reached this topic yet.

What I understood from all this const reference assignment is:
Even if the const version is called in that assignment - the compiler somehow "casts" this const reference to int - to the int value itself (let's call it a copy in practice).

Of course, in our particular case we can even assign to m1 something because it's non-const object. If it were a const Matrix object - we can't even assign to it anything.

That's in principle what I understood...
Thank you!

6. @laserlight -
Actually, I'm attending now the C++ class, and the instructor says that we declare for example two versions of operator[] such that:
reference operator[](size_type n)
const_reference operator[](size_type n) const

And he is saying the first one is for when we call the operator[] in case the object is in the left side, and the const version is for when we call this operator in case the object is in the right side.

But you have said that as long as the objects are non-const - the operator which will be called is the non-const one, no matter in which side any one of them.

What's going on here?...

7. Originally Posted by HelpMeC
What I understood from all this const reference assignment is:
Even if the const version is called in that assignment - the compiler somehow "casts" this const reference to int - to the int value itself (let's call it a copy in practice).
I would discourage understanding it as a cast. Rather, remember that a reference is an alias, i.e., it is another name for what it refers to. Furthermore, if you were to implement a copy assignment operator for a class type, you will find that canonically the parameter is of const reference type.

Originally Posted by HelpMeC
Of course, in our particular case we can even assign to m1 something because it's non-const object. If it were a const Matrix object - we can't even assign to it anything.
Yes, but remember that that's because the const overload for operator() is designed such that such assignment to change the content of the matrix is impossible.

Originally Posted by HelpMeC
And he is saying the first one is for when we call the operator[] in case the object is in the left side, and the const version is for when we call this operator in case the object is in the right side.

But you have said that as long as the objects are non-const - the operator which will be called is the non-const one, no matter in which side any one of them.

What's going on here?...
Your instructor is mistaken. You can prove that by writing a program, or changing one of the programs already discussed here to show that for this expression:
Code:
`m1(0, 0) = m2(0, 0);`
where both m1 and m2 are non-const, the non-const operator() is called twice.

EDIT:
For example, you could compile and run this program:
Code:
```#include <iostream>
#include <vector>

class Vector
{
public:
typedef int value_type;
typedef std::size_t size_type;
typedef value_type& reference;
typedef const value_type& const_reference;

explicit Vector(size_type size) : data(size) {}

reference operator[](size_type i)
{
std::cout << "non-const" << std::endl;
return data[i];
}

const_reference operator[](size_type i) const
{
std::cout << "const" << std::endl;
return data[i];
}
private:
std::vector<value_type> data;
};

int main()
{
Vector v1(1);
Vector v2(1);
v1[0] = v2[0];
}```

8. @laserlight -
You are of course right... I have shown your comment to my instructor, what led him to mail all the students he had a mistake explaining it that way.

I think I got the big pic, but it seems like I can ask forever questions about that, like, okay, for the const version of this operator we return const reference which is just aliasing - so we are returning actually the very value of the reference eventually. So it becomes like a copy of that int assigned to another integer.
But, we are returning const reference - but the constness of this reference has no effect on this copying process to my understanding - as we're eventually assigning a copy of the value..

Thank you so much, you are amazing!!!!!!!!!!!!!!

9. Originally Posted by HelpMeC
@laserlight -
You are of course right... I have shown your comment to my instructor, what led him to mail all the students he had a mistake explaining it that way.

I think I got the big pic, but it seems like I can ask forever questions about that, like, okay, for the const version of this operator we return const reference which is just aliasing - so we are returning actually the very value of the reference eventually. So it becomes like a copy of that int assigned to another integer.
But, we are returning const reference - but the constness of this reference has no effect on this copying process to my understanding - as we're eventually assigning a copy of the value..

Thank you so much, you are amazing!!!!!!!!!!!!!!
Now I think I understand the necessity of the const reference I talked about in the above comment.
If a const member will invoke this kind of operator, and we for example return some of its data by reference, we want its data to be constant, so that's the reason to const the reference!