美文网首页C/C++学习笔记
4.0 C++远征:重载运算符

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

作者: 青_阳 | 来源:发表于2016-11-18 18:37 被阅读14次

重载运算符

[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 <ostream>
    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指针。

相关文章

  • 慕课网-C++远征之模板篇(下)-学习笔记

    C++远征之模板篇(下) 运算符重载 给原有的运算符赋予新功能 + 数字相加。重载为字符串拼接 举个栗子: 上述代...

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

    重载运算符 [TOC] 四、重载运算符 ​ 概念 : 给原有运算符赋予新功能。 ​ 本质 : 函数重载。 ...

  • 第十一章 使用类

    运算符重载 运算符重载是一种形式的C++多态。运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。...

  • 1.2.15_C++ 关系运算符重载

    C++ 重载运算符和重载函数 C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),...

  • C++ 运算符重载

    运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。实际上,很多C++运算符已经重载。将*运算符用...

  • C++运算符重载

    C++运算符重载的实质:运算符重载的实质就是函数重载或函数多态。运算符重载是一种形式的C++多态。目的在于让人能够...

  • C++运算符重载-下篇 (Boolan)

    C++运算符重载-下篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++运算符重载-上篇 (Boolan)

    C++运算符重载-上篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++ 重载运算符

    C++重载运算符

  • C++重载

    重载 C++语言规定: 重载的运算符要保持原运算符的意义。只能对已有的运算符重载,不能增加新的运算符。重载的运算符...

网友评论

    本文标题:4.0 C++远征:重载运算符

    本文链接:https://www.haomeiwen.com/subject/tpqspttx.html