zoukankan      html  css  js  c++  java
  • 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.

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Test
     5 {
     6     int x;
     7     int &ref;
     8 public:
     9     Test (int i):x(i), ref(x) 
    10     {
    11     }
    12     void print() 
    13     { 
    14         cout << ref; 
    15     }
    16     void setX(int i) 
    17     { 
    18         x = i; 
    19     }    
    20 };
    21 
    22 int main()
    23 {
    24     Test t1(10);
    25     Test t2(20);
    26     t2 = t1;
    27     t1.setX(40);
    28     t2.print();
    29     return 0;
    30 }

      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.

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Test
     5 {
     6     int x;
     7     int &ref;
     8 public:
     9     Test (int i):x(i), ref(x) 
    10     {
    11     }
    12     void print() 
    13     { 
    14         cout << ref; 
    15     }
    16     void setX(int i) 
    17     { 
    18         x = i; 
    19     }    
    20     Test &operator = (const Test &t) 
    21     { 
    22         x = t.x; 
    23         return *this; 
    24     } 
    25 };
    26 
    27 int main()
    28 {
    29     Test t1(10);
    30     Test t2(20);
    31     t2 = t1;
    32     t1.setX(40);
    33     t2.print();
    34     return 0;
    35 }

      Output:  10

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

      转载请注明:http://www.cnblogs.com/iloveyouforever/

      2013-11-26   22:01:46

  • 相关阅读:
    记事本02
    助人快乐:笔记本连网
    高性能 架构实例 学习笔记
    食.运动.阅读
    The server name ... address could not be resolved
    Mysql 远程访问
    CSS布局 UI 学习笔记
    MySql 修改root密码
    C#:String类型中的CharAt 方法
    La_Lb_Lc
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3444396.html
Copyright © 2011-2022 走看看