• 浅谈C++运算符重载


    首先,什么是运算符重载?

    C++里运算符重载主要有两种方法。一是通过类的成员函数进行重载,二是通过类的友元函数进行重载。

    下面以简单的复数类complex为例:

    下面是通过类的成员函数进行运算符的重载。

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 
     5 /*运算符重载可以通过类的成员函数和友元函数两种途径来实现*/
     6 
     7 class complex
     8 {
     9     public:
    10         complex(int x=0,int y=0)
    11         {
    12             real = x;
    13             image = y;
    14         }
    15         ~complex(){}
    16         void show()
    17         {
    18             if(image >= 0)
    19                 cout<<real<<"+"<<image<<"i"<<endl;   
    20             else
    21                 cout<<real<<image<<"i"<<endl;   
    22         }
    23         /*重载双目运算符*/
    24         complex operator +(const complex& a);
    25         complex operator -(const complex& a);
    26         /*重载单目运算符*/
    27         complex operator ++(); //原本为一个参数,但是少一个参数就没有参数了。
    28         complex operator ++(int);
    29     private:
    30         int real,image;
    31 };
    32 
    33 /*注意:以成员函数的形式来重载运算符,此时参数要比原本的参数少一个,因为在非静态成员函数中有this指针访问该对象*/
    34 /*但是如果是用友元函数来实现运算符重载,那么运算符函数的参数个数就是操作数的个数*/
    35 
    36 complex complex::operator +(const complex& a)
    37 {
    38     return complex(real+a.real,image+a.image);
    39 }
    40 
    41 complex complex::operator -(const complex& a)
    42 {
    43     complex c;
    44     c.real = real - a.real;
    45     c.image = image - a.image;
    46     return c;
    47 }
    48 
    49 /*默认为不带参数为前置运算符,带参数int为后置运算符重载*/
    50 complex complex::operator ++()  //前置++
    51 {
    52     complex c;
    53     real++;
    54     image++;
    55     c.real = real;
    56     c.image = image;
    57     return c;
    58 }
    59 
    60 complex complex::operator ++(int) //后置++
    61 {
    62     complex c;
    63     c.real = real;
    64     c.image = image;
    65     real++;
    66     image++;
    67     return c;
    68 }
    69 
    70 /*之前对为什么可以直接调用complex类型的对象的私有变量表示疑惑,我个人理解是可以在complex的成员函数中调用任何complex类的对象的私有变量*/
    71 
    72 int main()
    73 {
    74     complex a1(1,2);
    75     complex a2(2,3);
    76     complex c1,c2,c3,c4;
    77     c1 = a1 + a2;
    78     c2 = a1 - a2;    
    79     c3 = ++c1;
    80     c4 = c2++;
    81     c1.show();
    82     c2.show();
    83     c3.show();
    84     c4.show();
    85     return 0;
    86 }

    也可以通过友元函数来进行重载。比较懒,所以直接截MOOC上的图了。

    通过以上的例子,简单的说明了运算符重载,帮助理解运算符的重载。

    不过得注意,运算符重载有一些要求:

     

    如果有错误,欢迎批评指正!

  • 相关阅读:
    Convert string to hex « Python recipes « ActiveState Code
    分享:301定向技术全面解决
    UTF8 ANSI Unicode/UCS2 UCS4 所占存储比较_高山流水_百度空间
    分享:Arcadia 0.12.1 发布,Ruby 集成开发环境
    分享:探索 Python、机器学习和 NLTK 库
    Windows Codepage: 936 (Simplified Chinese GBK)
    分享:QT 5.0 正式版发布,支持 C++11
    分享:探索 Python、机器学习和 NLTK 库
    分享:Gearmand 1.0.2 和 1.1.4 发布,作业调度系统
    Convert string to hex « Python recipes « ActiveState Code
  • 原文地址:https://www.cnblogs.com/jeavenwong/p/8120201.html
Copyright © 2020-2023  润新知