• 【C++】类和对象(中下)


      前言:

            上一篇文章,我们了解了类的默认成员函数——构造函数、析构函数、拷贝构造函数,这篇我们让我们接着了解。

    一、赋值运算符重载

    1.运算符重载:

            C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也就是说它跟普通的函数一样具有返回值类型,安徽念书名字以及参数列表。

     函数名字:关键字(operator)后面接需要重载的运算符符号;

    函数原型:返回值类型operator操作符(参数列表)。

    ps:

    ①.只能在原有的运算符基础上重载,不能通过连接其他符号来创建新的操作符,比如:operator@、operator#等等都不行;

    ②.重载操作符必须有一个类类型参数;

    ③用于内置类型的运算符,其含义不能改变,例如:内置的整形+,不能改变其含义;

    ④作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this指针。

    *::sizeof?: , . 这五个操作符不能重载。

    1. class Date
    2. {
    3. public:
    4. Date(int year=2023,int month=9,int day=15)
    5. {
    6. _year=year;
    7. _month=month;
    8. _day=day;
    9. }
    10. public:
    11. //这里因为运算符重载成全局的需要成员函数是公有的,后期可以用友元解决,或者重载成成员函数;
    12. int _year;
    13. int _month;
    14. int _day;
    15. };
    16. bool opprator==(const Date& d1,const Date& d2)
    17. {
    18. return d1._year=d2._year&&d1._month==d2._month&&d1._day==d2._day;
    19. }
    20. void Test()
    21. {
    22. Date d1(2002,09,21);
    23. Date d2;
    24. cout<<(d1==d2)<
    25. }

    在类里面实现:

    1. class Date
    2. {
    3. public:
    4. Date(int year=2023,int month=9,int day=15)
    5. {
    6. _year=year;
    7. _month=month;
    8. _day=day;
    9. }
    10. bool opprator==(const Date& d2)
    11. {
    12. return _year=d2._year&&_month==d2._month&&_day==d2._day;
    13. }
    14. private
    15. int _year;
    16. int _month;
    17. int _day;
    18. };
    19. void Test()
    20. {
    21. Date d1(2002,09,21);
    22. Date d2;
    23. cout<<(d1==d2)<
    24. }

    2.赋值运算符重载:

    Ⅰ.赋值运算符重载格式:

    参数类型:const T&,传递引用可以提高传参效率;

    返回值类型:T&,返回引用可以提高返回效率,有返回值目的是为了支持连续赋值;

    检测是否给自己赋值;

    返回*this。

    1. class Date
    2. {
    3. public:
    4. Date(int year=2023,int month=9,int day=15)
    5. {
    6. _year=year;
    7. _month=month;
    8. _day=day;
    9. }
    10. bool oprator==(const Date& d2)
    11. {
    12. return _year=d2._year&&_month==d2._month&&_day==d2._day;
    13. }
    14. Date& oprator=(const Date& d)
    15. {
    16. if(this!=&d)
    17. {
    18. _year=d._year;
    19. _month=d.month;
    20. _day=d._day;
    21. }
    22. return *this;
    23. }
    24. private
    25. int _year;
    26. int _month;
    27. int _day;
    28. };

    Ⅱ赋值运算符只能重载成类的成员函数不能重载成全局函数:

    1. class Date
    2. {
    3. public:
    4. Date(int year=2023,int month=9,int day=15)
    5. {
    6. _year=year;
    7. _month=month;
    8. _day=day;
    9. }
    10. bool oprator==(const Date& d2)
    11. {
    12. return _year=d2._year&&_month==d2._month&&_day==d2._day;
    13. }
    14. private
    15. int _year;
    16. int _month;
    17. int _day;
    18. };
    19. Date& oprator=(const Date& d1,const Date& d2)
    20. {
    21. if(&d2=&d1)
    22. {
    23. d1._year=d2._year;
    24. d1._month=d2.month;
    25. d1._day=d2._day;
    26. }
    27. return d1;
    28. }
    29. void Test()
    30. {
    31. Date d1(2002,09,21);
    32. Date d2;
    33. cout<<(d1==d2)<
    34. }

            上面这段代码会编译失败:error C2801:“operator=”必须是非静态成员。

    原因:赋值运算符如果不显示实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,所以赋值运算符只能是类成员函数。

    编译器生成的默认赋值运算符重载是以值的方式逐字节拷贝

    Ⅲ.默认赋值运算符重载

            上面我们讲到认编译器生成的默认赋值运算符重载是以值的方式逐字节拷贝。对于日期类来说,逐字节拷贝可以完成相应功能,但是有些情况逐字节拷贝无法完成,所以就需要深拷贝,现在我们来看看深拷贝的情况。

    1. class Stack
    2. {
    3. public:
    4. Stack(size_t capacity = 10)
    5. {
    6. _array = (int*)malloc(capacity * sizeof(int));
    7. if (nullptr == _array)
    8. {
    9. perror("llmalloc申请空间失败");
    10. return;
    11. }
    12. _size = 0;
    13. _capacity = capacity;
    14. }
    15. void Push(const int& data)
    16. {
    17. //检查需要扩容,但这只是演示一下深拷贝情况,所以就不写了
    18. _array[_size] = data;
    19. _size++;
    20. }
    21. ~Stack()
    22. {
    23. if (_array)
    24. {
    25. free(_array);
    26. _array = nullptr;
    27. _capacity = 0;
    28. _size = 0;
    29. }
    30. }
    31. private:
    32. int* _array;
    33. size_t _size;
    34. size_t _capacity;
    35. };
    36. int main()
    37. {
    38. Stack s;
    39. s.Push(1);
    40. s.Push(2);
    41. s.Push(3);
    42. Stack s1;
    43. s1 = s;
    44. return 0;
    45. }

    上述代码会造成两个问题:

    ①指向同一块空间,但s改变的时候s1也会跟着改变。

    ②当程序结束的时候会调用两次析构函数,同一块空间析构两次,这也是导致程序奔溃的主要原因。

    3.前置++和后置++重载:

            同样我们用日期类来看看前置++和后置++:

    1. class Date
    2. {
    3. public:
    4. Date(int year=2023,int month=9,int day=15)
    5. {
    6. _year=year;
    7. _month=month;
    8. _day=day;
    9. }
    10. Date(const Date& d)
    11. {
    12. _year=d._year;
    13. _month=d._month;
    14. _day=d._day;
    15. }
    16. //前置++
    17. Date& operator++()
    18. {
    19. _day++;
    20. return *this;
    21. }
    22. //前置++
    23. //C++规定:后置++重载时多增加一个整形参数,但调用函数时该参数不用传递,编译器自动传递
    24. Date operator++(int)
    25. {
    26. 因为前置++返回的是没有改变前的值,所以要备份一个tmp
    27. 且因为出了作用域后tmp会摧毁,所以返回值不能使用引用
    28. Date tmp(*this);
    29. _day++;
    30. return tmp;
    31. }
    32. private
    33. int _year;
    34. int _month;
    35. int _day;
    36. };

    4.const成员:

    将const修饰的成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针。

    5.取地址及const取地址操作符重载:

            这两个默认成员函数一般不用重新定义,编译器默认会生成,只有特殊情况才需要重载,比如想让别人读取到指定内容。

    总结:

            文章到这,我们关于类的六个默认成员函数已经介绍完了,也是构造函数,析构函数,拷贝构造函数,赋值运算符重载,const成员函数,取地址及const取地址操作符重载。

  • 相关阅读:
    流式编程 stream
    Redis概述与安装
    手把手教你用IDEA搭建SpringBoot并使用部署宝塔服务器
    二硫化钨/二硒化钨纳米复合材料|WSe2二硒化钨负载掺氮三维石墨烯|氮掺杂二氧化钛负载二硒化钨|二硒化钨薄片/氧化铟纳米线复合材料
    上帝视角一览大数据开发体系
    Git推送和拉取Github
    广汽传祺E9上市,3DCAT实时云渲染助力线上3D高清看车体验
    汇智动力《软件测试课程V8.0版本》正式发布!
    Js逆向教程-03浏览器调试工具-Source面板
    基于JAVA的鲜花店商城平台【数据库设计、源码、开题报告】
  • 原文地址:https://blog.csdn.net/zz02_24/article/details/132911016