Can someone please explain the use of const when its being used in function declarations and classes? I understand that if a function returns a const variable/object, then that thing can not be used on the left side of an assignment operator? Is this correct? Also, i noticed that if you were to do:
int x = 1;
int &i = x;
The above code would not compile unless i were a "constant reference" to the int. For functions, do the arguments passed into the function parameters have to be constant if the parameters are declared constant?
One example of my book has the following code:
const Array &Array::operator=(const Array &right)
{
if (&right != this) //avoid self assignment
//blah blah
return *this; //enables x = y = z for example
}
Why does this function permit the object Array assignments such as x = y = z but prevents ones like (x = y) = z?? The book's reasoning is "because z cannot be assigned to the const Array reference that's returned by (x = y). But in the first case, doesn't x = y also return a constant Array reference???
Also, when you do assign 1 object of a class to another, the compiler uses member wise assignment unless the the user provides a default copy constructor, in which case THAT is invoked right?
Thankyou so much! Sorry I have a lot of specific questions. I greatly appreciate it.