Default Assignment Operator and References

We have discussed assignment operator overloading for dynamically allocated resources here . This is a an extension of the previous post. In the previous post, we discussed that when we don’t write our own assignment operator, compiler created assignment operator does shallow copy and that cause problems. What happens when we have references in our class and there is no user defined assignment operator. For example, predict the output of following program.

#include<iostream>
using namespace std;
 
class Test
{
    int x;
    int &ref;
public:
    Test (int i):x(i), ref(x) {}
    void print() { cout << ref; }
    void setX(int i) { x = i; }    
};
 
int main()
{
    Test t1(10);
    Test t2(20);
    t2 = t1;
    t1.setX(40);
    t2.print();
    return 0;
}

Output:

Compiler Error: non-static reference member 'int& Test::ref', 
             can't use default assignment operator

Compiler doesn’t creates default assignment operator in following cases

1. Class has a nonstatic data member of a const type or a reference type
2. Class has a nonstatic data member of a type which has an inaccessible copy assignment operator
3. Class is derived from a base class with an inaccessible copy assignment operator

When any of the above conditions is true, user must define assignment operator. For example, if we add an assignment operator to the above code, the code works fine without any error.

#include<iostream>
using namespace std;
 
class Test
{
    int x;
    int &ref;
public:
    Test (int i):x(i), ref(x) {}
    void print() { cout << ref; }
    void setX(int i) { x = i; }    
    Test &operator = (const Test &t) { x = t.x; return *this; } 
};
 
int main()
{
    Test t1(10);
    Test t2(20);
    t2 = t1;
    t1.setX(40);
    t2.print();
    return 0;
}

Output:

10

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.





  • Ashish

    Actually When I am writing
    Test a(10);
    Test c = a;
    a.setX(20);

    //c.x is 10 and c.ref = 20;
    why so???

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
    • anonymous

      Actually a copy constructor is not provided , so the default copy constructor is automatically invoked by the compiler ,which does member wise copy copying the ref of this to ref of the object ,which inturn point to x of the object ,so changing x of the object changes ref of this.

  • Vikas

    Point 2 is not clear…

    please clarify it with some example..

     
    /* Paste your code here (You may delete these lines if not writing code) */
     
    • Kartik
       
      #include<iostream>
      using namespace std;
      
      class Test1
      {
      private:
          Test1 & operator = (Test1 const &t) {}
      };
      
      class Test2
      {
          Test1 t;
          // We must overload assignment operator for Test2
          // because it has a nonstatic data member t
          // which has an inaccessible copy assignment operator
      
      };
      
      int main()
      {
          Test2 t1;
          Test2 t2;
          t2 = t1;   // Compiler error because of this line
          return 0;
      }