Friday, 15 June 2012

c++ - Default assignment operator in inner class with reference members -


I have participated in a problem I did not understand and I was hoping that somebody here has some insights May be provided. Simplified code is as follows (original code was a custom queue / queue-iterator implementation):

  class B {public: b () {}; Class C {Public: int get (); C (B & B): B (B) {}; Private: B & amp; B; }; Public: C get_c () {return C (* this); }}; Int main () {B b; B :: cc = bgt_c (); C = b.get_c (); Return EXIT_SUCCESS; }  

This, when compiled, I get the following error:

  foo.cpp: member function 'B :: C & amp; ; B :: C :: Operator = (Const B :: C & amp;) ': foo.cpp: 46: Error: Non-Static Reference Member' B & amp; B :: C :: B 'can not use the default assignment operator, foo.cpp:' int main () 'in the function: foo.cpp: 63: Note: The synthesized method is' B :: C & amp; B :: C :: Operator = (Const B :: C & amp;) 'First is required here  

I can go around using two different variables Because they should be free 'C' objects, but it only hides the problem (I still can not understand why I can not do this).

I think the reference can not be copied, why does T understand that I need to provide my own assignment operator and copy the constructor?

Internal classes of this problem. In C ++ you can not only assign (re-assign) - they need to be started when defined.

A simple example:

  class B {public: B (int and eye): IR (i) {}; Integer & amp; Ir; }; Int main () {int i; BB (I); // Constructor - Okay Int J; Bbb = b (j); // copy constructor - fine BB = b; // Assignment - Error 0; }  

No comments:

Post a Comment