Let's say I define the following operator:
const int & operator= (const MyClass &);
Can I use it to make an assignment to a non-const variable?
Reading through the comments here, my understanding is that yes, I can (i.e. I could do things like a=b
even if a is not const
and the compiler wouldn't complailn).
However, when I try the following code:
int main()
{
int x = 42;
const int &y = x; // y is a const reference to x
int &z = y;
}
It fails with the following:
compilation
execution
main.cpp:9:8: error: binding reference of type 'int' to value of type 'const int' drops 'const' qualifier
int &z = y;
^ ~
1 error generated.