From: Chris Uzdavinis on
On May 14, 10:51 am, "Daya S. Prasad" <mail....(a)gmail.com> wrote:

[snip]
> error: non-static reference member 'B& TestClass::obj', can't use
> default assignment operator
> note: synthesized method 'TestClass& TestClass::operator=(const
> TestClass&)' first required here
>
> Can anybody please tell me exactly what's the reason for compilation
> error. As per my understanding this error comes due to B &
> TestClass::obj but reference is also an object with different name and
> B can use its default assignment operator for assignment.

Not quite. While you are expecting it to assign data through the
reference, what the default assignment operator would want to do is
actually assign (rebind) the reference itself, and C++ does only
allows references to be bound during their initialization... you
cannot change the object to which a reference refers, and so the
auto-generated assignment operator is invalid and thus not allowed.

Other things also prevent default assignment operators
* members w/o assignment operators or inaccessible ones
* const members
* references
etc.

Chris


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]