鱼C论坛

 找回密码
 立即注册
查看: 2359|回复: 0

[学习笔记] 032-C++之运算符重载1

[复制链接]
发表于 2018-9-2 17:15:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1、简介
C++可以重载大部分内置的运算符来实现对象之间的操作。
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
1. 不能重载的运算符
        .:成员访问运算符
        .*, ->*:成员指针访问运算符
        :::域运算符
        sizeof:长度运算符
        ?::条件运算符
        #: 预处理符号
2.运算符重载的限制
重载运算符函数可以对运算符作出新的解释,但原有基本语义不变:
        ①不改变运算符的优先级
        ②不改变运算符的结合性
        ③不改变运算符需要的操作数
        ④不能创建新的运算符
3.运算符重载两种方式
成员函数重载:
        ObjectL.operator op (ObjectR)
其左操作数通过this指针传递,右操作数需要有参数传递,一元操作符时,缺省。
友元函数重载:
        operator op (ObjectL, ObjectR)
友元函数重载时其左右操作数都需参数提供。
注意: =   ()    []     ->  操作符不能用友元函数重载
          cout<< 重载是只能用友元函数
2、+ 的成员函数重载、- 的友元函数重载
  1. class Complex       // 复数类
  2. {
  3. public:               // 重载操作符需要写到public里
  4.         friend Complex operator-(Complex &c1, Complex &c2);        // 友元重载的友元声明
  5.         Complex operator+(Complex &c2)     //c1隐藏于this指针     // 成员函数重载
  6.         {
  7.                 Complex tmp;
  8.                 tmp.a = this->a + c2.a;
  9.                 tmp.b = this->b + c2.b;
  10.                 return tmp;                  //返回时调用拷贝构造函数
  11.         }

  12.         Complex(int a = 0, int b = 0)
  13.         {
  14.                 this->a = a;
  15.                 this->b = b;
  16.         }

  17.         void print()
  18.         {
  19.                 cout << a << "i+" << b << endl;
  20.         }
  21. protected:
  22. private:
  23.         int a;
  24.         int b;
  25. };
  26. Complex operator-(Complex &c1, Complex &c2)    //友元重载的定义
  27. {
  28.         Complex c3;
  29.         c3.a = c1.a - c2.a;
  30.         c3.b = c1.b - c2.b;
  31.         return c3;
  32. }

  33. int main()
  34. {
  35.         Complex c1(1, 2), c2(3, 4), c3, c4;
  36.         c3 = c1+ c2;
  37.         c4 = c1 - c2;
  38.         c3.print();
  39.         c4.print();
  40.         system("pause");
  41.         return 0;
  42. }
复制代码

3、前缀--的成员函数重载、前缀++的友元函数重载
  1. class Complex       // 复数类
  2. {
  3. public:            
  4.         friend Complex & operator++(Complex &c2);   // 友元重载前缀++声明
  5.         Complex operator--()   // 成员重载实现前缀--
  6.         {
  7.                 --this->a;
  8.                 --this->b;
  9.                 return *this;
  10.         }

  11.         Complex(int a = 0, int b = 0)
  12.         {
  13.                 this->a = a;
  14.                 this->b = b;
  15.         }

  16.         void print()
  17.         {
  18.                 cout << a << "i+" << b << endl;
  19.         }
  20. protected:
  21. private:
  22.         int a;
  23.         int b;
  24. };

  25. Complex & operator++(Complex &c2)   // 重载前缀++实现
  26. {
  27.         c2.a++;
  28.         c2.b++;
  29.         return c2;
  30. }

  31. int main()
  32. {
  33.         Complex c1(1, 2), c2(3, 4), c3, c4;
  34.         c3 = ++c1;
  35.         c4 = --c2;
  36.         c1.print();
  37.         c2.print();
  38.         c3.print();
  39.         c4.print();
  40.         system("pause");
  41.         return 0;
  42. }
复制代码

4、后缀--的成员函数重载、后缀++的友元函数重载
C++为了区分前缀和后缀,规定后缀时需要加上int这个占位参数。
  1. class Complex       // 复数类
  2. {
  3. public:               // 重载操作符需要写到public里
  4.         friend Complex operator++(Complex &c2, int);   // 友元重载后缀++声明
  5.         Complex operator--(int)             // 成员重载实现后缀--
  6.         {
  7.                 Complex tmp = *this;
  8.                 this->a--;
  9.                 this->b--;
  10.                 return tmp;

  11.         }

  12.         Complex(int a = 0, int b = 0)
  13.         {
  14.                 this->a = a;
  15.                 this->b = b;
  16.         }

  17.         void print()
  18.         {
  19.                 cout << a << "i+" << b << endl;
  20.         }
  21. protected:
  22. private:
  23.         int a;
  24.         int b;
  25. };

  26. Complex operator++(Complex &c2, int)   // 重载后缀++实现
  27. {
  28.         // 先使用c2的属性,再让属性++, 即返回++之前c2的值
  29.         Complex tmp;
  30.         tmp = c2;
  31.         c2.a++;
  32.         c2.b++;
  33.         return tmp;
  34. }

  35. int main()
  36. {
  37.         Complex c1(1, 2), c2(3, 4), c3, c4;
  38.         c3 = c1++;
  39.         c4 = c2--;
  40.         c1.print();
  41.         c2.print();
  42.         c3.print();
  43.         c4.print();
  44.         system("pause");
  45.         return 0;
  46. }
复制代码

5、cout<< 的友元重载及它的链式编程
cout.operator<<(Complex &c1), cout不会转化为this指针,及它不能重载为成员函数,只能采用友元重载的方法。
为了使“cout<<a<<b<<endl;”得到实现,必须让重载的返回只能够做左值,让返回值做左值,需要返回引用。
  1. class Complex       // 复数类
  2. {
  3. public:     
  4.         friend ostream& operator<<(ostream &out, Complex &c1);   // cou<<的友元重载声明
  5.         Complex(int a = 0, int b = 0)
  6.         {
  7.                 this->a = a;
  8.                 this->b = b;
  9.         }

  10.         void print()
  11.         {
  12.                 cout << a << "i+" << b << endl;
  13.         }
  14. protected:
  15. private:
  16.         int a;
  17.         int b;
  18. };
  19. // 返回引用,可以做左值
  20. ostream& operator<<(ostream &out, Complex &c1) // cou<<的友元重载实现  
  21. {
  22.         cout << c1.a << "i+" << c1.b << endl;
  23.         return out;
  24. }

  25. int main()
  26. {
  27.         Complex c1(1, 2);
  28.         cout << c1 << "链式编程测试" << endl;
  29.         system("pause");
  30.         return 0;
  31. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 07:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表