• C++核心编程——4.5 运算符重载


    4.5.0 运算符重载概念

       对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

    4.5.1 加号运算符重载

    作用:实现两个自定义数据类型相加的运算

    1. class Person {
    2. public:
    3. Person() {};
    4. Person(int a, int b)
    5. {
    6. this->m_A = a;
    7. this->m_B = b;
    8. }
    9. //1.成员函数实现 + 号运算符重载
    10. Person operator+(const Person& p) {
    11. Person temp;
    12. temp.m_A = this->m_A + p.m_A;
    13. temp.m_B = this->m_B + p.m_B;
    14. return temp;
    15. }
    16. public:
    17. int m_A;
    18. int m_B;
    19. };
    20. //2.全局函数实现 + 号运算符重载(class+class)
    21. //Person operator+(const Person& p1, const Person& p2) {
    22. // Person temp(0, 0);
    23. // temp.m_A = p1.m_A + p2.m_A;
    24. // temp.m_B = p1.m_B + p2.m_B;
    25. // return temp;
    26. //}
    27. //运算符重载 可以发生函数重载 (class+int)
    28. Person operator+(const Person& p2, int val)
    29. {
    30. Person temp;
    31. temp.m_A = p2.m_A + val;
    32. temp.m_B = p2.m_B + val;
    33. return temp;
    34. }
    35. void test() {
    36. Person p1(10, 10);
    37. Person p2(10, 10);
    38. //成员函数方式
    39. Person p3 = p2 + p1; //成员函数的本质调用 Person p3 = p2.operaor+(p1)
    40. cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;
    41. //全局函数方式
    42. Person p4 = p3 + p2; //全局函数的本质调用 Person p4 = operator+(p3,p2)
    43. cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;
    44. //全局函数重载
    45. Person p5 = p4 + 10; //全局函数的本质调用 Person p4 = operator+(p4,10)
    46. cout << "mA:" << p5.m_A << " mB:" << p5.m_B << endl;
    47. }
    48. int main() {
    49. test();
    50. system("pause");
    51. return 0;
    52. }

    总结1:对于内置的数据类型的表达式的的运算符是不可能改变的(int/double等的改变不了) 

    总结2:不要滥用运算符重载 

    4.5.2 左移运算符重载

    作用:可以输出自定义数据类型

    1. class Person {
    2. friend ostream& operator<<(ostream& out, Person& p);
    3. public:
    4. Person(int a, int b)
    5. {
    6. this->m_A = a;
    7. this->m_B = b;
    8. }
    9. //成员函数:
    10. //成员函数 void operator<<(Person& p)
    11. //实现的是 p.operator<<(p),需要两个对象,有一个对象再传入一个对象(错误的)
    12. //若成员函数为 void operator<<(cout) 简化版本为p << cout
    13. //实现不了 cout<< 不是我们想要的效果
    14. //总结:通常不会用成员函数重载左移运算符(无法实现cout在左侧)
    15. private:
    16. int m_A;
    17. int m_B; //通常写一个类的时候把成员属性设置为私有化,又要让全局函数访问,所以用友元
    18. };
    19. //全局函数实现左移重载
    20. // void operator << (cout , p) 该行代码本质为operator << (cout , p) ,简化为cout<
    21. //不知道什么返回类型统一先写void
    22. //ostream(output stream:输出流对象)对象只能有一个,所以用引用的方式(不能创建新的)
    23. ostream& operator<<(ostream& cout, Person& p) {
    24. cout << "a:" << p.m_A << " b:" << p.m_B;
    25. return cout;
    26. }
    27. //输出要继续用链式编程的方法,必须继续返回ostream,调用之后还返回cout才可以继续链式编程
    28. void test() {
    29. Person p1(10, 20);
    30. cout << p1 << "链式编程" << endl; //链式编程
    31. }
    32. int main() {
    33. test();
    34. system("pause");
    35. return 0;
    36. }

    总结:重载左移运算符配合友元可以实现输出自定义数据类型  

    4.5.3 递增运算符重载

    作用: 通过重载递增运算符,实现自己的整型数据

    1. class MyInteger {
    2. friend ostream& operator<<(ostream& out, MyInteger myint);
    3. public:
    4. MyInteger() {
    5. m_Num = 0;
    6. }
    7. //前置++
    8. MyInteger& operator++() {
    9. //返回值也没有错,但是会返回新的值,在新的值上面进行操作,原来的值只会进行第一次操作
    10. //返回值: cout<< ++(++myint) << endl; 输出结果为:2
    11. // cout<< myint << endl; 1(对新对象递增,原对象不变)
    12. //返回引用:cout<< ++(++myint) << endl; 输出结果为:2
    13. // cout<< myint << endl; 2
    14. //先++
    15. m_Num++;
    16. //再返回
    17. return *this; //this指向自身,*this对自身解引用 (返回本身)
    18. //如果返回的是void,语法变成cout << void,仍然不知道返回的是什么,所以依然需要返回int
    19. }
    20. //后置++
    21. //(后置递增返回值不返回引用,若后置返回引用,相当于返回的是一个局部的引用,而局部对象在当前函数执行之后就被释放了,如果再返回引用就是违法操作,所以后置递增一定返回的是值)
    22. MyInteger operator++(int) { //为了发生函数重载必须增加形参int(占位参数,用于区分前置和后置)
    23. //先返回
    24. MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
    25. m_Num++;
    26. return temp; //如果直接return写在前面,函数执行到return就会返回;
    27. }
    28. private:
    29. int m_Num;
    30. };
    31. //重载左移运算符
    32. ostream& operator<<(ostream& cout, MyInteger myint) {
    33. cout << myint.m_Num;
    34. return cout;
    35. }
    36. //前置++ 先++ 再返回
    37. void test01() {
    38. MyInteger myInt;
    39. cout << ++myInt << endl;
    40. cout << myInt << endl;
    41. }
    42. //后置++ 先返回 再++
    43. void test02() {
    44. MyInteger myInt;
    45. cout << myInt++ << endl; //后置递增先输出表达式,在++操作
    46. cout << myInt << endl;
    47. }
    48. int main() {
    49. test01();
    50. //test02();
    51. system("pause");
    52. return 0;
    53. }

    总结: 前置递增返回引用,后置递增返回值  

    4.5.4 赋值运算符重载

    c++编译器至少给一个类添加4个函数

    1. 默认构造函数(无参,函数体为空)

    2. 默认析构函数(无参,函数体为空)

    3. 默认拷贝构造函数,对属性进行值拷贝

    4. 赋值运算符 operator=, 对属性进行值拷贝

    如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

    1. class Person
    2. {
    3. public:
    4. Person(int age)
    5. {
    6. //将年龄数据开辟到堆区,用指针维护堆区的数据
    7. //关键字new返回的是一个地址
    8. m_Age = new int(age);
    9. }
    10. //重载赋值运算符 重载运算:p2 = p1,当p2去调用函数时需要传入一个p1,所以要传一个person类
    11. Person& operator=(Person &p)
    12. {
    13. //编译器提供的代码是浅拷贝
    14. //m_Age = p.m_Age;
    15. //深拷贝必须先判断p2本身上面是否有数据,有的话要先置空,再深拷贝
    16. //要把p1的数据都给p1,但是如果p2本身有数据,就会出错
    17. if (m_Age != NULL)
    18. {
    19. delete m_Age;
    20. m_Age = NULL;
    21. }
    22. //提供深拷贝重新new一块空间
    23. m_Age = new int(*p.m_Age); //用自身的指针指向new的新的地址
    24. //返回自身
    25. return *this; //this是指针,需要解引用才能返回本身
    26. //返回值如果是void只能进行一次赋值,如果是p3 = p2 = p1,语法上为p3 = void报错
    27. }
    28. ~Person()
    29. {
    30. if (m_Age != NULL)
    31. {
    32. delete m_Age;
    33. m_Age = NULL;
    34. }
    35. }
    36. //年龄的指针
    37. int *m_Age;
    38. };
    39. void test01()
    40. {
    41. Person p1(18);
    42. Person p2(20);
    43. Person p3(30);
    44. p3 = p2 = p1; //赋值操作
    45. cout << "p1的年龄为:" << *p1.m_Age << endl; //p1.m_Age为指针,所以必须解引用
    46. cout << "p2的年龄为:" << *p2.m_Age << endl;
    47. cout << "p3的年龄为:" << *p3.m_Age << endl;
    48. }
    49. int main() {
    50. test01();
    51. //int a = 10;
    52. //int b = 20;
    53. //int c = 30;
    54. //c = b = a;
    55. //cout << "a = " << a << endl;
    56. //cout << "b = " << b << endl;
    57. //cout << "c = " << c << endl;
    58. system("pause");
    59. return 0;
    60. }

    4.5.5 关系运算符重载

    作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

    1. class Person
    2. {
    3. public:
    4. Person(string name, int age)
    5. {
    6. this->m_Name = name;
    7. this->m_Age = age;
    8. };
    9. bool operator==(Person & p)
    10. {
    11. if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
    12. {
    13. return true;
    14. }
    15. else
    16. {
    17. return false;
    18. }
    19. }
    20. bool operator!=(Person & p)
    21. {
    22. if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
    23. {
    24. return false;
    25. }
    26. else
    27. {
    28. return true;
    29. }
    30. }
    31. string m_Name;
    32. int m_Age;
    33. };
    34. void test01()
    35. {
    36. //int a = 0;
    37. //int b = 0;
    38. Person a("库里", 18);
    39. Person b("乔丹", 18);
    40. if (a == b)
    41. {
    42. cout << "a和b相等" << endl;
    43. }
    44. else
    45. {
    46. cout << "a和b不相等" << endl;
    47. }
    48. if (a != b)
    49. {
    50. cout << "a和b不相等" << endl;
    51. }
    52. else
    53. {
    54. cout << "a和b相等" << endl;
    55. }
    56. }
    57. int main() {
    58. test01();
    59. system("pause");
    60. return 0;
    61. }

    4.5.6 函数调用运算符重载

    • 函数调用运算符 “()” 也可以重载

    • 由于重载后使用的方式非常像函数的调用,因此称为仿函数

    • 仿函数没有固定写法,非常灵活

    1. class MyPrint
    2. {
    3. public:
    4. void operator()(string text)
    5. {
    6. cout << text << endl;
    7. }
    8. };
    9. void Print(string text) //函数
    10. {
    11. cout << text << endl;
    12. }
    13. void test01()
    14. {
    15. //重载的()操作符 也称为仿函数
    16. MyPrint myFunc;
    17. myFunc("hello world"); //重载的()操作符:使用的方式非常像函数的调用
    18. Print("hello world"); //函数的调用
    19. }
    20. //仿函数实现非常灵活,实现加法
    21. class MyAdd
    22. {
    23. public:
    24. int operator()(int v1, int v2)
    25. {
    26. return v1 + v2;
    27. }
    28. };
    29. void test02()
    30. {
    31. MyAdd add;
    32. int ret = add(10, 10);
    33. cout << "ret = " << ret << endl;
    34. //匿名对象调用(可以不创建对象直接调用)
    35. //用MyAdd()创建匿名对象,当前行执行完之后立马被释放
    36. cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
    37. }
    38. int main() {
    39. test01();
    40. test02();
    41. system("pause");
    42. return 0;
    43. }

     

  • 相关阅读:
    Linux驱动开发——PCI设备驱动
    独立站市场回暖,三个角度深度剖析广阔前景
    基于JAVA旅游信息网站计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    2023大连海洋大学计算机考研信息汇总
    HCIA网络基础9-VRP文件系统管理
    leetcode82删除排序链表中的重复元素
    GitHub 的基本使用
    UE4 制作十字准心+后坐力
    【linux基础】 5. apt安装原理, apt相关指令, 换源(更新Ubuntu软件下载地址)
    快速上手Django(六) -Django之Django drf 序列化器Serializer类
  • 原文地址:https://blog.csdn.net/2301_81926399/article/details/138171014