当前位置:   article > 正文

【学习笔记】C++ 运算符重载_运算符重载是为了改变语法结构

运算符重载是为了改变语法结构

运算符重载是使用户可以对已有的运算符进行重载,从而实现对自定义的数据类型的运算操作

  • 1、运算重载符不可以改变语法结构。
  • 2、运算重载符不可以改变操作数的个数。
  • 3、运算重载符不可以改变优先级。
  • 4、运算重载符不可以改变结合性。

1.加号运算符重载

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Student
  5. {
  6. public:
  7. Student(int age)
  8. {
  9. this->age = age; //等价于Student::age = age;
  10. //this指针指向被调用的成员函数所属的对象
  11. };
  12. int age;
  13. //成员函数作运算符重载
  14. Student &operator+(Student &s1)
  15. {
  16. this->age += s1.age;
  17. return *this;
  18. };
  19. private:
  20. string arr = "你是大傻逼";
  21. };
  22. //全局函数作运算符重载
  23. Student operator+(Student &s1, Student &s2)
  24. {
  25. Student temp(0);
  26. temp.age = s1.age + s2.age;
  27. return temp;
  28. };
  29. int main()
  30. {
  31. Student s1(10);
  32. Student s2(10);
  33. Student s3 = s1 + s2;
  34. cout << s3.age << endl;
  35. system("pause");
  36. return 0;
  37. }

2.左移运算符

与cout结合运用,实现输出自定义数据类型.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Student
  5. {
  6. friend ostream &operator<<(Student &s1,ostream &cout);
  7. friend ostream &operator<<(ostream &cout,Student &s1);
  8. public:
  9. Student(int age)
  10. {
  11. this->age = age; //等价于Student::age = age;
  12. //this指针指向被调用的成员函数所属的对象
  13. };
  14. int age;
  15. //成员函数作运算符重载
  16. /* Student &operator+(Student &s1)
  17. {
  18. this->age += s1.age;
  19. return *this;
  20. };
  21. */
  22. private:
  23. string arr = "你是大傻逼";
  24. };
  25. //全局函数作运算符重载
  26. Student operator+(Student &s1, Student &s2)
  27. {
  28. Student temp(0);
  29. temp.age = s1.age + s2.age;
  30. return temp;
  31. };
  32. ostream &operator<<( Student &s1,ostream &cout)
  33. {
  34. cout << "student age = " << s1.age <<"student arr = "<<s1.arr<< endl;
  35. return cout;
  36. }
  37. ostream &operator<<(ostream &cout,Student &s1)
  38. {
  39. cout << "student age = " << s1.age <<"student arr = "<<s1.arr<< endl;
  40. return cout;
  41. }
  42. int main()
  43. {
  44. Student s1(10);
  45. Student s2(10);
  46. Student s3 = s1 + s2;
  47. s3 << cout << endl;//ostream &operator<<( Student &s1,ostream &cout)
  48. cout<<s3;//ostream &operator<<(ostream &cout,Student &s1)
  49. system("pause");
  50. return 0;
  51. }

注意以下代码实现的区别

  1. ostream &operator<<(Student &s1,ostream &cout);
  2. ostream &operator<<(ostream &cout,Student &s1);

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

4.赋值运算符重载

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

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

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

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

5.关系运算符重载

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Student
  5. {
  6. friend ostream &operator<<(Student &s1, ostream &cout);
  7. friend ostream &operator<<(ostream &cout, Student &s1);
  8. public:
  9. Student(int age)
  10. {
  11. this->age = age; //等价于Student::age = age;
  12. //this指针指向被调用的成员函数所属的对象
  13. };
  14. bool operator==(Student &s1)
  15. {
  16. if (this->age == s1.age && this->arr == s1.arr)
  17. return true;
  18. else
  19. return false;
  20. };
  21. int age;
  22. //成员函数作运算符重载
  23. /* Student &operator+(Student &s1)
  24. {
  25. this->age += s1.age;
  26. return *this;
  27. };
  28. */
  29. private:
  30. string arr = "你是大傻逼";
  31. };
  32. //全局函数作运算符重载
  33. Student operator+(Student &s1, Student &s2)
  34. {
  35. Student temp(0);
  36. temp.age = s1.age + s2.age;
  37. return temp;
  38. };
  39. ostream &operator<<(Student &s1, ostream &cout)
  40. {
  41. cout << "student age = " << s1.age << "student arr = " << s1.arr << endl;
  42. return cout;
  43. }
  44. ostream &operator<<(ostream &cout, Student &s1)
  45. {
  46. cout << "student age = " << s1.age << "student arr = " << s1.arr << endl;
  47. return cout;
  48. }
  49. int main()
  50. {
  51. Student s1(10);
  52. Student s2(10);
  53. Student s3 = s1 + s2;
  54. if (s1 == s2)
  55. {
  56. cout << "这俩相等" << endl; /* code */
  57. }
  58. s3 << cout << endl; //ostream &operator<<( Student &s1,ostream &cout)
  59. cout << s3; //ostream &operator<<(ostream &cout,Student &s1)
  60. system("pause");
  61. return 0;
  62. }

6.()运算符重载&仿函数

  • 函数调用运算符 () 也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
    1. class MyPrint
    2. {
    3. public:
    4. void operator()(string text)
    5. {
    6. cout << text << endl;
    7. }
    8. };
    9. void test01()
    10. {
    11. //重载的()操作符 也称为仿函数
    12. MyPrint myFunc;
    13. myFunc("hello world");
    14. }
    15. class MyAdd
    16. {
    17. public:
    18. int operator()(int v1, int v2)
    19. {
    20. return v1 + v2;
    21. }
    22. };
    23. void test02()
    24. {
    25. MyAdd add;
    26. int ret = add(10, 10);
    27. cout << "ret = " << ret << endl;
    28. //匿名对象调用
    29. cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
    30. }
    31. int main() {
    32. test01();
    33. test02();
    34. system("pause");
    35. return 0;
    36. }

    MyAdd():匿名对象,Myadd类型的一个对象,在代码执行后内存立即释放.

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号