【C++深度剖析学习总结】 23 操作符重载
所谓重载,就是赋予新的含义。函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作。运算符重载(Operator Overloading)也是一个道理,同一个运算符可以有不同的功能。
 实际上,我们已经在不知不觉中使用了运算符重载。例如,+号可以对不同类型(int、float 等)的数据进行加法操作;<<既是位移运算符,又可以配合 cout 向控制台输出数据。C++ 本身已经对这些运算符进行了重载。
 C++ 也允许程序员自己重载运算符,这给我们带来了很大的便利。
下面的代码定义了一个复数类,通过运算符重载,可以用+号实现复数的加法运算:
  
   - 
    
     
    
    
     
      #include <iostream>
     
    
 
   - 
    
     
    
    
     
      using namespace std;
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      class complex{
     
    
 
   - 
    
     
    
    
     
      public:
     
    
 
   - 
    
     
    
    
         complex();
     
    
 
   - 
    
     
    
    
         complex(double real, double imag);
     
    
 
   - 
    
     
    
    
     
      public:
     
    
 
   - 
    
     
    
    
         //声明运算符重载
     
    
 
   - 
    
     
    
    
     
          complex operator+(const complex &A) const;
     
    
 
   - 
    
     
    
    
         void display() const;
     
    
 
   - 
    
     
    
    
     
      private:
     
    
 
   - 
    
     
    
    
         double m_real;  //实部
     
    
 
   - 
    
     
    
    
         double m_imag;  //虚部
     
    
 
   - 
    
     
    
    
     
      };
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      complex::complex(): m_real(0.0), m_imag(0.0){ }
     
    
 
   - 
    
     
    
    
     
      complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      //实现运算符重载
     
    
 
   - 
    
     
    
    
     
      complex complex::operator+(const complex &A) const{
     
    
 
   - 
    
     
    
    
     
          complex B;
     
    
 
   - 
    
     
    
    
     
          B.m_real = this->m_real + A.m_real;
     
    
 
   - 
    
     
    
    
     
          B.m_imag = this->m_imag + A.m_imag;
     
    
 
   - 
    
     
    
    
         return B;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      void complex::display() const{
     
    
 
   - 
    
     
    
    
     
          cout<<m_real<<" + "<<m_imag<<"i"<<endl;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      int main(){
     
    
 
   - 
    
     
    
    
         complex c1(4.3, 5.8);
     
    
 
   - 
    
     
    
    
         complex c2(2.4, 3.7);
     
    
 
   - 
    
     
    
    
     
          complex c3;
     
    
 
   - 
    
     
    
    
     
          c3 = c1 + c2;
     
    
 
   - 
    
     
    
    
     
          c3.display();
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
         return 0;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
  
 
运行结果:
 6.7 + 9.5i
 本例中义了一个复数类 complex,m_real 表示实部,m_imag 表示虚部,第 10 行声明了运算符重载,第 21 行进行了实现(定义)。认真观察这两行代码,可以发现运算符重载的形式与函数非常类似。
 运算符重载其实就是定义一个函数,在函数体内实现想要的功能,当用到该运算符时,编译器会自动调用这个函数。也就是说,运算符重载是通过函数实现的,它本质上是函数重载。
运算符重载函数除了函数名有特定的格式,其它地方和普通函数并没有区别。
上面的运算符重载还可以有更加简练的定义形式:
  
   - 
    
     
    
    
     
      complex complex::operator+(const complex &A)const{
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      return complex(this->m_real + A.m_real, this->m_imag + A.m_imag);
     
    
 
  
 
return 语句中的
 
complex(this->m_real + A.m_real, this->m_imag + A.m_imag)
 
会创建一个临时对象,这个对象没有名称,是一个匿名对象。在创建临时对象过程中调用构造函数,return 语句将该临时对象作为函数返回值。
 
在全局范围内重载运算符
运算符重载函数不仅可以作为类的成员函数,还可以作为全局函数。更改上面的代码,在全局范围内重载+,实现复数的加法运算:
  
   - 
    
     
    
    
     
      #include <iostream>
     
    
 
   - 
    
     
    
    
     
      using namespace std;
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      class complex{
     
    
 
   - 
    
     
    
    
     
      public:
     
    
 
   - 
    
     
    
    
         complex();
     
    
 
   - 
    
     
    
    
         complex(double real, double imag);
     
    
 
   - 
    
     
    
    
     
      public:
     
    
 
   - 
    
     
    
    
         void display() const;
     
    
 
   - 
    
     
    
    
         //声明为友元函数
     
    
 
   - 
    
     
    
    
         friend complex operator+(const complex &A, const complex &B);
     
    
 
   - 
    
     
    
    
     
      private:
     
    
 
   - 
    
     
    
    
         double m_real;
     
    
 
   - 
    
     
    
    
         double m_imag;
     
    
 
   - 
    
     
    
    
     
      };
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      complex operator+(const complex &A, const complex &B);
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      complex::complex(): m_real(0.0), m_imag(0.0){ }
     
    
 
   - 
    
     
    
    
     
      complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
     
    
 
   - 
    
     
    
    
     
      void complex::display() const{
     
    
 
   - 
    
     
    
    
     
          cout<<m_real<<" + "<<m_imag<<"i"<<endl;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      //在全局范围内重载+
     
    
 
   - 
    
     
    
    
     
      complex operator+(const complex &A, const complex &B){
     
    
 
   - 
    
     
    
    
     
          complex C;
     
    
 
   - 
    
     
    
    
     
          C.m_real = A.m_real + B.m_real;
     
    
 
   - 
    
     
    
    
     
          C.m_imag = A.m_imag + B.m_imag;
     
    
 
   - 
    
     
    
    
         return C;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      int main(){
     
    
 
   - 
    
     
    
    
         complex c1(4.3, 5.8);
     
    
 
   - 
    
     
    
    
         complex c2(2.4, 3.7);
     
    
 
   - 
    
     
    
    
     
          complex c3;
     
    
 
   - 
    
     
    
    
     
          c3 = c1 + c2;
     
    
 
   - 
    
     
    
    
     
          c3.display();
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
         return 0;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
  
 
1.需要解决的问题
下面的复数解决方案是否可行?

复数的加法操作
  
   - 
    
     
    
    
     
      #include <stdio.h>
     
    
 
   - 
    
     
    
    
     
      class Complex
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
         int a;//hide variable
     
    
 
   - 
    
     
    
    
         int b;
     
    
 
   - 
    
     
    
    
     
      public:
     
    
 
   - 
    
     
    
    
         Complex(int a = 0, int b = 0)
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             this->a = a;
     
    
 
   - 
    
     
    
    
             this->b = b;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         int getA()
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             return a;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         int getB()
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             return b;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         friend Complex Add(const Complex& p1, const Complex& p2);
     
    
 
   - 
    
     
    
    
     
      };
     
    
 
   - 
    
     
    
    
     
      Complex Add(const Complex& p1, const Complex& p2)
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
     
          Complex ret;
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
     
          ret.a = p1.a + p2.a;
     
    
 
   - 
    
     
    
    
     
          ret.b = p1.b + p2.b;
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         return ret;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
   - 
    
     
    
    
     
      int main()
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
         Complex c1(1, 2);
     
    
 
   - 
    
     
    
    
         Complex c2(3, 4);
     
    
 
   - 
    
     
    
    
     
          Complex c3 = Add(c1, c2); // c1 + c2
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         return 0;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
  
 
2.思考
Add函数可以解决Complex对象相加的问题,但是Complex是现实世界中确实存在的复数,并且复数在数学中的地位和普通的实数相同。
为什么不让+操作符也支持复数相加呢?
3.操作符重载
C++中的重载能够扩展操作符的功能
操作符的重载以函数的方式进行
本质:
用特殊形式的函数扩展操作符的功能
通过operator关键字可以定义特殊的函数
operator的本质是通过函数重载操作符
语法:

Sign为系统中预定义的操作符,如:+,-,*,/,等
操作符重载初探--全局函数版本的实现
  
   - 
    
     
    
    
     
      #include <stdio.h>
     
    
 
   - 
    
     
    
    
     
      class Complex
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
         int a;
     
    
 
   - 
    
     
    
    
         int b;
     
    
 
   - 
    
     
    
    
     
      public:
     
    
 
   - 
    
     
    
    
         Complex(int a = 0, int b = 0)
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             this->a = a;
     
    
 
   - 
    
     
    
    
             this->b = b;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         int getA()
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             return a;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         int getB()
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             return b;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         friend Complex operator + (const Complex& p1, const Complex& p2);//友元函数
     
    
 
   - 
    
     
    
    
     
      };//类定义之后又;
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      Complex operator + (const Complex& p1, const Complex& p2)
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
     
          Complex ret;
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
     
          ret.a = p1.a + p2.a;
     
    
 
   - 
    
     
    
    
     
          ret.b = p1.b + p2.b;
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         return ret;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      int main()
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
         Complex c1(1, 2);
     
    
 
   - 
    
     
    
    
         Complex c2(3, 4);
     
    
 
   - 
    
     
    
    
     
          Complex c3 = c1 + c2; // operator + (c1, c2)
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         return 0;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
  
 
可以将操作符重载函数定义为类的成员函数
-  
- 比全局操作符重载函数少一个参数(左操作数)this充当左操作数
 -  
- 不需要依赖友元就可以完成操作符重载
 -  
- 编译器优先在成员函数中寻找操作符重载函数
 

成员函数重载操作符--成员函数调用
  
   - 
    
     
    
    
     
      #include <stdio.h>
     
    
 
   - 
    
     
    
    
     
      class Complex
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
         int a;
     
    
 
   - 
    
     
    
    
         int b;
     
    
 
   - 
    
     
    
    
     
      public:
     
    
 
   - 
    
     
    
    
         Complex(int a = 0, int b = 0)
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             this->a = a;
     
    
 
   - 
    
     
    
    
             this->b = b;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         int getA()
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             return a;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         int getB()
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
             return b;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
     
          Complex operator + (const Complex& p)    //成员函数版本---优先调用
     
    
 
   - 
    
     
    
    
     
          {
     
    
 
   - 
    
     
    
    
     
              Complex ret;
     
    
 
   - 
    
     
    
    
             printf("Complex operator + (const Complex& p)\n");
     
    
 
   - 
    
     
    
    
     
              ret.a = this->a + p.a;
     
    
 
   - 
    
     
    
    
     
              ret.b = this->b + p.b;
     
    
 
   - 
    
     
    
    
            
     
    
 
   - 
    
     
    
    
             return ret;
     
    
 
   - 
    
     
    
    
     
          }
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         friend Complex operator + (const Complex& p1, const Complex& p2);
     
    
 
   - 
    
     
    
    
     
      };
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      Complex operator + (const Complex& p1, const Complex& p2)     //全局函数版本
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
     
          Complex ret;
     
    
 
   - 
    
     
    
    
         printf("Complex operator + (const Complex& p1, const Complex& p2)\n");
     
    
 
   - 
    
     
    
    
     
          ret.a = p1.a + p2.a;
     
    
 
   - 
    
     
    
    
     
          ret.b = p1.b + p2.b;
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         return ret;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
   - 
    
     
    
    
      
     
    
 
   - 
    
     
    
    
     
      int main()
     
    
 
   - 
    
     
    
    
     
      {
     
    
 
   - 
    
     
    
    
         Complex c1(1, 2);
     
    
 
   - 
    
     
    
    
         Complex c2(3, 4);
     
    
 
   - 
    
     
    
    
     
          Complex c3 = c1 + c2; // c1.operator + (c2) ,通过成员函数实现
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
     
    
 
   - 
    
     
    
    
        
     
    
 
   - 
    
     
    
    
         return 0;
     
    
 
   - 
    
     
    
    
     
      }
     
    
 
  
 
小结
操作符重载是C++的强大特性之一
操作符重载的本质是通过函数扩展操作符的功能
operator关键字是实现操作符重载的关键
操作符重载遵循相同的函数重载规则
全局函数和成员函数都可以实现对操作符的重载
文章来源: allen5g.blog.csdn.net,作者:CodeAllen的博客,版权归原作者所有,如需转载,请联系作者。
原文链接:allen5g.blog.csdn.net/article/details/117452621
- 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)