Is it necessary that the assignment operator (=) always copies the rvalue into the lvalue or is it completely dependent on the type of operands that it is operating on? Sorry if it is silly but whatever I read, it is written that it just copies the value on the right to the left. I want to get my facts right.
Please give an example in C++.
No. For user-defined types it does no such thing. Rather, it invokes the appropriate
operator=function. Consider these classes:As you can see, the assignment operator is free to perform whatever actions it sees fit. In this case, it is clearly not merely a “copy”.