c/c++賦值函數(重載=號運算符)


c/c++賦值函數(重載=號運算符)

首先c++里的各種運算符都是用函數實現的,比如=,就等號函數。

所以當用=給一個對象賦值的時候,實際調用的是=號所對應的=號函數。

分析下面的代碼

#include <iostream>
using namespace std;

class Test{
public:
  explicit Test(){
    data = 0;
  }
  explicit Test(int d):data(d){
    cout << "C:" << this << ":"<< this->data << endl;
  }
  //拷貝構造函數                                                        
  Test(const Test &t){
    cout << "Copy:" << this << endl;
    data = t.data;
  }
  //重載=號運算符                                            
  Test& operator= (const Test &t){
    cout << "assign" << this << endl;
    if(this != &t){
      data = t.data;
    }
    return *this;
  }
  ~Test(){
    cout << "F:" << this << ":" << this->data << endl;
  }
private:
  int data;
};
int main(){
  Test t1(10);
  Test t2, t3;

  t3 = t2 = t1;

  return 0;
}

重點分析下面的函數

  //重載=號運算符                                            
  Test& operator = (const Test &t){
    cout << "assign" << this << endl;
    if(this != &t){
      data = t.data;
    }
    return *this;
  }

分析點:

1,operator =是什么意思

2,參數為什么是引用類型

3,參數為什么有const限制

4,為什么有if(this != &t)的判斷

5,為什么有返回值

6,為什么返回值的類型是引用類型

分析點解答:

Test t2;

t2 = t1;//實際的運作方式是t2.operator=(t1),所以函數里面的this就是t2

1,重載類Test的=號函數,當對類Test的對象用=號操作的時候,就會調用這個重載后的函數

2,避免調用拷貝構造函數

3,避免不小心修改里參數t里面成員變量的值(t.data = 100;)

4,防止自己給自己賦值

5,為了能夠使用 t3 = t2 = t1。如果沒有返回值,則t3.operator=(t2=t1),的參數里面t2=t1就沒有返回值,所以編譯不過。

6,不是引用也可以,用引用類型是防止老版本的編譯器,在return處調用拷貝構造函數,新版本的編譯器(gcc 4.8.5-20),即使不用引用類型,就不會調用拷貝構造函數。

一個大大的疑問,求高人指點

下面代碼,重載了=號函數,故意把fun函數的返回值類型設置為引用,t2 = fun(t1);的執行結果:t2的data還是0,符合常理。

#include <iostream>
using namespace std;

class Test{
public:
  Test(int d = 0):data(d){
    cout << "C:" << d << " " << this << endl;
  }
  Test(Test &t){
    cout << "Copy:" << t.data << " " << this << endl;
    data = t.data;
  }

  Test& operator = (const Test &t){
    cout << "Assign:" << this << " = " << &t << endl;
    if(this != &t){
      data = t.data;
    }
    return *this;
    }

  ~Test(){
    cout << "F:" << this->data << "->" << this << endl;
  }
  int getData()const{
    return data;
  }
private:
  int data;
};


Test& fun(Test &x){
  int value = x.getData();
  Test tmp(value);
  return tmp;

}

int main(){
  Test t1(100);
  Test t2;
  t2 = fun(t1);
  cout << t2.getData() << endl;
  //Test t2 = fun(t1);                                                

  return 0;
}

但是,注釋掉重載的=號函數,故意把fun函數的返回值類型設置為引用,t2 = fun(t1);的執行結果:t2的data居然是100,不符合常理,求高人指點

#include <iostream>
using namespace std;

class Test{
public:
  Test(int d = 0):data(d){
    cout << "C:" << d << " " << this << endl;
  }
  Test(Test &t){
    cout << "Copy:" << t.data << " " << this << endl;
    data = t.data;
  }
  /*
  Test& operator = (const Test &t){
    cout << "Assign:" << this << " = " << &t << endl;
    if(this != &t){
      data = t.data;
    }
    return *this;
    }
  */

  ~Test(){
    cout << "F:" << this->data << "->" << this << endl;
  }
  int getData()const{
    return data;
  }
private:
  int data;
};


Test& fun(Test &x){
  int value = x.getData();
  Test tmp(value);
  return tmp;

}

int main(){
  Test t1(100);
  Test t2;
  t2 = fun(t1);
  cout << t2.getData() << endl;
  //Test t2 = fun(t1);                                                

  return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM