handling reference member variable in assignmnet operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • msgurikar
    New Member
    • Jan 2010
    • 2

    handling reference member variable in assignmnet operator

    Hi,
    How do i handle reference member variable in assignment operator and copy constructor.

    Code:
    class A{
    private:
    int& a;
    public:
    A();
    ~A();
    A(const A& a1);
    A& operator=(const A& a1);
    };
    
    A::A():a(0){}
    A::A(int& i): a(i)  //yes,, we have to do this
    {
    }
    same way, how do i handle reference member variable in copy constructor and assignment operator implementation,

    Regards
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The in copy constructor you just initialise the reference of the new object from the reference of the old object.

    You treat it as an other member, you assign to it if you want to being aware that you will change the object being referenced not the reference itself.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Code:
      A::A(const A& a1) : a(a1.a)
      {
      }
      
      A::A& operator=(const A& a1)
      {
          a = a1.a; // If desired
      
          // ... rest of operator= code
      }

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        A::A():a(0){}

        this gives an error on gcc - it can't initialize the reference pointing to non-variable. And you also can't re-initialize the reference once it's initialized in constructor - so the assignment can't change it, only the variable it points to.

        Comment

        • msgurikar
          New Member
          • Jan 2010
          • 2

          #5
          A& A::operator=(co nst A& a1)
          {

          a = a1.a // Is it a initialization or assigning
          }


          Is the above statmeent in assignment operator is initialization or assignmnet,
          according to me its assignment, not initialization. I dont think reference can be assigned, hope the above will give comiler error.

          Regards

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            operator= is an operator and works on an object. Therefore the object already exists and since initialisation happens when the object is create and this object already exists this must be assignment.

            a = a1.a;

            Will not give a compiler error as since a and a1.a have the same type, int.

            But like I (and you) said you can assign references so it will copy the object referenced by a1.a to the object referenced a rather than copy a1.a to a.

            Also like I said you will have to decide if this is desirable behaviour but it is certainly valid code and it may be desirable behaviour.

            If you need a reference that you can change in operator= then use a pointer rather than a reference or better yet a handle class.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              And don't forget that in your copy constructors and assignment operators you cannot copy or assgn the object to itself. However, your user can code it that way. Be sure you test that this!= &of the other object. If it is, just return. Otherwise, go ahead and make the copy.

              Comment

              Working...