• C++运算符重载


    作用:给运算符重新进行定义,赋予其另一种功能,以适应于不同的数据类型。

    加号运算符重载“+”

    对于内置的数据类型,编译器知道如何进行运算。例如int a=10, int b=20, c=a+b这种的。若有两个对象相加,那怎么办呢?可以用加号运算符重载。

    两个自定义数据类型相加的运算需要进行重载。

    例:两个对象的相加。

    case 1:通过成员函数实现两个对象的相加

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. public:
    7. Person PersonAddPerson(Person& p)
    8. {
    9. Person temp;
    10. temp.m_A = this->m_A + p.m_A;
    11. temp.m_B = this->m_B + p.m_B;
    12. return temp;
    13. }
    14. int m_A;
    15. int m_B;
    16. };
    17. int main()
    18. {
    19. Person p1;
    20. p1.m_A = 10;
    21. p1.m_B = 10;
    22. Person p2;
    23. p2.m_A = 20;
    24. p2.m_B = 20;
    25. p2.PersonAddPerson(p1);
    26. cout << p2.m_A << endl;
    27. cout << p2.m_B << endl;
    28. }

    case 2:成员函数重载+号运算符

    成员函数重载本质的调用

    Person p3=p1.operator+(p2);

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. public:
    7. //通过成员函数重载加号运算符
    8. Person operator+ (Person& p)
    9. {
    10. Person temp;
    11. temp.m_A = this->m_A + p.m_A;
    12. temp.m_B = this->m_B + p.m_B;
    13. return temp;
    14. }
    15. int m_A;
    16. int m_B;
    17. };
    18. int main()
    19. {
    20. Person p1;
    21. p1.m_A = 10;
    22. p1.m_B = 10;
    23. Person p2;
    24. p2.m_A = 20;
    25. p2.m_B = 20;
    26. Person p3 = p1 + p2;
    27. //等价于Person p3=p1.operator+(p2);
    28. }

    case 3:全局函数重载+

    全局函数重载的本质调用:Person p3=operator+(p1,p2);

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. public:
    7. int m_A;
    8. int m_B;
    9. };
    10. //通过全局函数重载加号运算符
    11. Person operator+ (Person& p1,Person& p2)
    12. {
    13. Person temp;
    14. temp.m_A = p1.m_A + p2.m_A;
    15. temp.m_B = p1.m_B + p2.m_B;
    16. return temp;
    17. }
    18. int main()
    19. {
    20. Person p1;
    21. p1.m_A = 10;
    22. p1.m_B = 10;
    23. Person p2;
    24. p2.m_A = 20;
    25. p2.m_B = 20;
    26. Person p3 = p1 + p2;
    27. }

    对于内置的数据类型的表达式的运算符是不可以改变的,且不要滥用运算符重载。

    左移运算符重载“<<”

    作用:配合友元,可以输出自定义的输出类型。

    1. #include<iostream>
    2. #include<vector>
    3. using namespace std;
    4. class Person {
    5. public:
    6. friend ostream& operator<< (ostream& cout, Person& p);
    7. Person(int a, int b)
    8. {
    9. m_A = a;
    10. m_B = b;
    11. }
    12. private:
    13. int m_A;
    14. int m_B;
    15. };
    16. //利用全局函数重载左移运算符
    17. ostream& operator<< (ostream& cout, Person &p)
    18. {
    19. //cout对象只能有一个,因此传个引用
    20. cout << p.m_A << p.m_B;
    21. return cout;
    22. }
    23. void test01()
    24. {
    25. Person p(10,10);
    26. cout<<p<<endl;//第2个<<是链式编程,因此第一次重载时需要返回cout的值
    27. }
    28. int main()
    29. {
    30. test01();
    31. }

    递增运算符重载“++”

    作用:通过递增运算符,实现自己的整形数据。具体地,包括前置递增和后置递增。后置递增返回值,前置递增返回引用。

    1. #include<iostream>
    2. using namespace std;
    3. //重载递增运算符
    4. //自定义整形
    5. class MyInteger
    6. {
    7. public:
    8. MyInteger()
    9. {
    10. n_Num = 10;
    11. }
    12. friend ostream& operator<< (ostream& cout, MyInteger& myint);
    13. //重载++运算符(前置)
    14. MyInteger& operator++ ()
    15. {
    16. //先进行++运算。再返回自己的引用,返回引用是为了一直对一个数据进行递增操作
    17. n_Num++;
    18. return *this;
    19. }
    20. //重载++运算符(后置)
    21. MyInteger operator++ (int) //int的作用是代表占位参数,可以用于区分前置和后置递增
    22. {
    23. //先记录当时的结果
    24. MyInteger temp = *this;
    25. //后递增
    26. n_Num++;
    27. //最后将记录结果返回,由于返回的是局部变量,因此不能返回引用
    28. return temp;
    29. }
    30. private:
    31. int n_Num;
    32. };
    33. ostream& operator<< (ostream& cout, MyInteger& myint)
    34. {
    35. cout << myint.n_Num;
    36. return cout;
    37. }
    38. void test01()
    39. {
    40. MyInteger myint;
    41. cout << ++(++myint)<< endl;
    42. }
    43. void test02()
    44. {
    45. MyInteger myint;
    46. cout << (myint++) << endl;
    47. }
    48. int main()
    49. {
    50. test02();
    51. }

  • 相关阅读:
    JavaScript栈的实现与解题
    Cookie和Session的区别
    第28章 锁
    Golang实现组合模式和装饰模式
    Flink+Paimon多流拼接性能优化实战
    React(一)
    【owt】owt-client-native-p2p-e2e-test vs2017构建 3 : 无 测试单元对比, 手动生成vs项目
    面试经验——面试项目准备工作
    scrcpy笔记
    metaRTC5.0实现webrtc的TURN支持
  • 原文地址:https://blog.csdn.net/Lao_tan/article/details/125426743