Prev: cloning struct and derived structs with primitive type members
Next: [temp.arg.nontype] and & of non-static class members
From: Daya S. Prasad on 14 May 2010 00:51 Consider a code: class B { }; class TestClass { B &obj; public: TestClass(B &refObj) : obj(refObj) { cout << "Called ... TestClass(B &refObj)\n"; } TestClass & operator=(TestClass const &) { cout << "Called ... TestClass & operator=(TestClass const &)\n"; return *this; } ~TestClass() { cout << "Called ... ~TestClass()\n"; } }; B b_obj; TestClass obj1(b_obj), obj2(b_obj); obj1 = obj2; Above posted code works perfectly until and unless we've defined assignment operator. As soon as we remove overloaded assignment operator it throws compilation error. 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. Overall TestClass is perfectly assignable class without overloaded assignment operator. Then why compilation error? and why it removes when simply define overloaded assignment operator? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bo Persson on 14 May 2010 07:20
Daya S. Prasad wrote: > Consider a code: > > class B { }; > > class TestClass { > B &obj; > public: > TestClass(B &refObj) : obj(refObj) { > cout << "Called ... TestClass(B &refObj)\n"; > } > TestClass & operator=(TestClass const &) { > cout << "Called ... TestClass & operator=(TestClass const &)\n"; > return *this; > } > ~TestClass() { > cout << "Called ... ~TestClass()\n"; > } > }; > > B b_obj; > TestClass obj1(b_obj), obj2(b_obj); > obj1 = obj2; > > Above posted code works perfectly until and unless we've defined > assignment operator. As soon as we remove overloaded assignment > operator it throws compilation error. > > 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. > Overall TestClass is perfectly assignable class without overloaded > assignment operator. Then why compilation error? and why it removes > when simply define overloaded assignment operator? The reference is bound in the constructor, and cannot be reassigned. That makes it impossible to create the default assignment operator, as it would use memberwise assignment. Your user defined assignment operator "solves" this by not assigning any new value to the reference. In fact it doesn't do anything! Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |