1,加号运算符重载

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

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

总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
总结2:不要滥用运算符重载

2,左移运算符重载

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

  1. #include<iostream>
  2. using namespace std;
  3. class Person {
  4. friend ostream& operator<<(ostream& out, Person& p);
  5. public:
  6. Person(int a, int b)
  7. {
  8. this->m_A = a;
  9. this->m_B = b;
  10. }
  11. //成员函数 实现不了 p << cout 不是我们想要的效果
  12. //void operator<<(Person& p){
  13. //}
  14. private:
  15. int m_A;
  16. int m_B;
  17. };
  18. //全局函数实现左移重载 只能利用全局函数实现左移重载
  19. //ostream对象只能有一个
  20. ostream& operator<<(ostream& out, Person& p) {
  21. out << "a:" << p.m_A << " b:" << p.m_B<<endl;
  22. return out;
  23. }
  24. void test() {
  25. Person p1(10, 20);
  26. cout << p1 << "hello world" << endl; //链式编程
  27. }
  28. int main() {
  29. test();
  30. system("pause");
  31. return 0;
  32. }

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

3,递增运算符重载

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

  1. #include<iostream>
  2. using namespace std;
  3. class MyInteger {
  4. friend ostream& operator<<(ostream& out, MyInteger myint);
  5. public:
  6. MyInteger() {
  7. m_Num = 0;
  8. }
  9. //前置++
  10. MyInteger& operator++() {
  11. //先++
  12. m_Num++;
  13. //再返回
  14. return *this;
  15. }
  16. //后置++
  17. MyInteger operator++(int) {
  18. //先返回
  19. MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
  20. m_Num++;
  21. return temp;
  22. }
  23. private:
  24. int m_Num;
  25. };
  26. ostream& operator<<(ostream& out, MyInteger myint) {
  27. out << myint.m_Num;
  28. return out;
  29. }
  30. //前置++ 先++ 再返回
  31. void test01() {
  32. MyInteger myInt;
  33. cout << ++myInt << endl;
  34. cout << myInt << endl;
  35. }
  36. //后置++ 先返回 再++
  37. void test02() {
  38. MyInteger myInt;
  39. cout << myInt++ << endl;
  40. cout << myInt << endl;
  41. }
  42. int main() {
  43. test01();
  44. //test02();
  45. system("pause");
  46. return 0;
  47. }

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

4,赋值运算符重载

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

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符 operator=, 对属性进行值拷贝

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

示例

  1. #include<iostream>
  2. using namespace std;
  3. class Person
  4. {
  5. public:
  6. Person(int age)
  7. {
  8. //将年龄数据开辟到堆区
  9. m_Age = new int(age);
  10. }
  11. //重载赋值运算符
  12. Person& operator=(Person& p)
  13. {
  14. if (m_Age != NULL)
  15. {
  16. delete m_Age;
  17. m_Age = NULL;
  18. }
  19. //编译器提供的代码是浅拷贝
  20. //m_Age = p.m_Age;
  21. //提供深拷贝 解决浅拷贝的问题
  22. m_Age = new int(*p.m_Age);
  23. //返回自身
  24. return *this;
  25. }
  26. ~Person()
  27. {
  28. if (m_Age != NULL)
  29. {
  30. delete m_Age;
  31. m_Age = NULL;
  32. }
  33. }
  34. //年龄的指针
  35. int* m_Age;
  36. };
  37. void test01()
  38. {
  39. Person p1(18);
  40. Person p2(20);
  41. Person p3(30);
  42. p3 = p2 = p1; //赋值操作
  43. cout << "p1的年龄为:" << *p1.m_Age << endl;
  44. cout << "p2的年龄为:" << *p2.m_Age << endl;
  45. cout << "p3的年龄为:" << *p3.m_Age << endl;
  46. }
  47. int main() {
  48. test01();
  49. //int a = 10;
  50. //int b = 20;
  51. //int c = 30;
  52. //c = b = a;
  53. //cout << "a = " << a << endl;
  54. //cout << "b = " << b << endl;
  55. //cout << "c = " << c << endl;
  56. system("pause");
  57. return 0;
  58. }

5,关系运算符重载

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

示例

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

6,函数调用运算符重载

  • 函数调用运算符 () 也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活

示例

  1. #include<iostream>
  2. using namespace std;
  3. class MyPrint
  4. {
  5. public:
  6. void operator()(string text)
  7. {
  8. cout << text << endl;
  9. }
  10. };
  11. void test01()
  12. {
  13. //重载的()操作符 也称为仿函数
  14. MyPrint myFunc;
  15. myFunc("hello world");
  16. }
  17. class MyAdd
  18. {
  19. public:
  20. int operator()(int v1, int v2)
  21. {
  22. return v1 + v2;
  23. }
  24. };
  25. void test02()
  26. {
  27. MyAdd add;
  28. int ret = add(10, 10);
  29. cout << "ret = " << ret << endl;
  30. //匿名对象调用
  31. cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
  32. }
  33. int main() {
  34. test01();
  35. test02();
  36. system("pause");
  37. return 0;
  38. }

**