4.0 C++远征:重载运算符

重载运算符

[TOC]

四、重载运算符

概念 : 给原有运算符赋予新功能。

本质 : 函数重载。

关键字 : operator

1.一元运算符重载

符号只与一个操作数进行运算。

Ⅰ -(负号)的重载(取反啊)

  • 友元函数重载

    // Coordinate.hpp
    class Coordinate {  
      friend Coordinate& operator-(Coordinate &coor);
      public:
        Coordinate(int x, int y);
      private:
        int m_iX;
        int m_iY;
    };
    
    // Coordinate.cpp
    Coordinate& Coordinate::operator-(Coordinate &coor) {
      coor.m_iX = -coor.m_iX;
      coor.m_iY = -coor.m_iY;
      return *this;
    }
    
    // main.cpp
    int main() {
      Coordinate coor(3, 5);
      -coor;    // operator-(coor);
      
      return 0;
    }
    

  • 成员函数重载

    // Coordinate.hpp
    class Coordinate {
      public:
        Coordinate(int x, int y);
        Coordinate& operator-();    // 有this指针
      private:
        int m_iX;
        int m_iY;
    };
    
    // Coordinate.cpp
    Coordinate& Coordinate::operator-() {
      m_iX = -m_iX;
      m_iY = -m_iY;
      return *this;
    }
    
    // main.cpp
    int main() {
      Coordinate coor(3, 5);
      -coor;    // coor.operator-();
      
      return 0;
    }
    

Ⅱ ++符号的重载

  • 前置++符号重载

    // Coordinate.hpp
    class Coordinate {
      public:
        Coordinate(int x, int y);
        Coordinate& operator++();   // 前置++
      private:
        int m_iX;
        int m_iY;
    };
        
    // Coordinate.cpp
    Coordinate& Coordinate::operator++() {
      m_iX++;
      m_iY++;
      return *this;
    }
        
    // main.cpp
    int main() {
      Coordinate coor(3, 5);
      ++coor;   // coor.operator++();
          
      return 0;
    }
    
  • 后置++符号重载

    // Coordinate.hpp
    class Coordinate {
      public:
        Coordinate(int x, int y);
        Coordinate operator++(int); // 后置++(int用来标识当前的++是后置重载,并不接受任何入参)
      private:
        int m_iX;
        int m_iY;
    };
    
    // Coordinate.cpp
    Coordinate Coordinate::operator++(int) {
      Coordinate oldCoor(*this);
      m_iX++;
      m_iY++;
      return oldCoor;
    }
    
    // main.cpp
    int main() {
      Coordinate coor(3, 5);
      coor++;   // coor.operator++(0);
          
      return 0;
    }
    

2.二元运算符重载

符号与两个个操作数进行运算。

Ⅰ +(加号)的重载

  • 友元函数重载

    // Coordinate.hpp
    class Coordinate {
      friend Coordinate operator+(const Coordinate &coor1, const Coordinate &coor2);
      public:
        Coordinate(int x, int y);
      private:
        int m_iX;
        int m_iY;
    };
    
    // Coordinate.cpp
    Coordinate operator+(const Coordinate &coor1, const Coordinate &coor2) {
      Coordinate tempCoor;
      tempCoor.m_iX = coor1.m_iX + coor2.m_iX;
      tempCoor.m_iY = coor1.m_iY + coor2.m_iY;
      return tempCoor;
    }
    
    // main.cpp
    int main() {
      Coordinate coor1(3, 5);
      Coordinate coor2(4, 6);
      Coordinate coor3(0, 0);
      coor3 = coor1 + coor2;    // coor3 = operator+(coor1, coor2);
      
      return 0;
    }
    

  • 成员函数重载

    // Coordinate.hpp
    class Coordinate {
      public:
        Coordinate(int x, int y);
        Coordinate operator+(const Coordinate &coor);
      private:
        int m_iX;
        int m_iY;
    };
    
    // Coordinate.cpp
    Coordinate operator+(const Coordinate &coor) {
      Coordinate tempCoor;
      tempCoor.m_iX = this->m_iX + coor.m_iX;
      tempCoor.m_iY = this->m_iY + coor.m_iY;
      return tempCoor;
    }
    
    // main.cpp
    int main() {
      Coordinate coor1(3, 5);
      Coordinate coor2(4, 6);
      Coordinate coor3(0, 0);
      coor3 = coor1 + coor2;    // coor3 = coor1.operator+(coor2);
      
      return 0;
    }
    

Ⅱ <<号的重载

  • 友元函数重载

    // Coordinate.hpp
    #include 
    using namespace std;
    class Coordinate {
      friend ostream &operator<<(ostream &output, const Coordinate &coor);
      public:
        Coordinate(int x, int y);
      private:
        int m_iX;
        int m_iY;
    };
    
    // Coordinate.cpp
    ostream &operator<<(ostream &output, const Coordinate &coor) {
      output << coor.m_iX << ", " << coor.m_iY << endl;
      
      return output;
    }
    
    // main.cpp
    int main() {
      Coordinate coor(3, 5);
      
      cout << coor; // operator<<(cout, coor);
      
      return 0;
    }
    
  • 输出运算符可以采用成员函数重载吗?

    不可以。

    因为重载输出运算符的第一个参数必须是ostream &out,而若写成成员函数必须是this指针。

Ⅲ [](索引)的重载

// Coordinate.hpp
class Coordinate {
  public:
    Coordinate(int x, int y);  
    int operator[](int index);
  private:
    int m_iX;
    int m_iY;
};

// Coordinate.cpp
int operator[](int index) {
  if(index == 0)
    return m_iX;
  if(index == 1)
    return m_iY;
}

// main.cpp
int main() {
  Coordinate coor(3, 5);
  
  cout << coor[0];  // coor.operator[](0);
  cout << coor[1];  // coor.operator[](1);
  
  return 0;
}
  • 索引运算符可以采用友元函数重载吗?

    不可以。

    因为索引运算符的第一个参数必须是this指针。

你可能感兴趣的:(4.0 C++远征:重载运算符)