• C++心决之类和对象详解(中篇)(封装入门二阶)


    目录

    1.类的6个默认成员函数

    2. 构造函数

    2.1 概念

    2.2 特性

    3.析构函数

    3.1 概念

    3.2 特性

    4. 拷贝构造函数

    4.1 概念

    4.2 特征

    5.赋值运算符重载

    5.1 运算符重载

    5.2 赋值运算符重载

    5.3 前置++和后置++重载

    7.const成员

    8.取地址及const取地址操作符重载


    1.类的6个默认成员函数

    如果一个类中什么成员都没有,简称为空类。
    空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下 6 个默认成员
    函数。
    默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

    2. 构造函数

    2.1 概念

    对于以下 Date 类:
    1. class Date
    2. {
    3. public:
    4. void Init(int year, int month, int day)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. void Print()
    11. {
    12. cout << _year << "-" << _month << "-" << _day << endl;
    13. }
    14. private:
    15. int _year;
    16. int _month;
    17. int _day;
    18. };
    19. int main()
    20. {
    21. Date d1;
    22. d1.Init(2022, 7, 5);
    23. d1.Print();
    24. Date d2;
    25. d2.Init(2022, 7, 6);
    26. d2.Print();
    27. return 0;
    28. }
    对于 Date 类,可以通过 Init 公有方法给对象设置日期,但如果每次创建对象时都调用该方法设置
    信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?
    构造函数 是一个 特殊的成员函数,名字与类名相同 , 创建类类型对象时由编译器自动调用 ,以保证
    每个数据成员都有 一个合适的初始值,并且 在对象整个生命周期内只调用一次

    2.2 特性

    构造函数 是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任
    并不是开空间创建对象,而是初始化对象
    其特征如下:
    • 1. 函数名与类名相同。
    • 2. 无返回值。
    • 3. 对象实例化时编译器自动调用对应的构造函数。
    • 4. 构造函数可以重载。
    • 5. 如果类中没有显式定义构造函数,则 C++ 编译器会自动生成一个无参的默认构造函数,一旦
      用户显式定义编译器将不再生成。

    (需要注意的是,构造函数不能被显示调用)

    1.  class Date
    2. {
    3.  public:
    4.      // 1.无参构造函数
    5.      Date()
    6.     {}
    7.  
    8.      // 2.带参构造函数
    9.      Date(int year, int month, int day)
    10.     {
    11.          _year = year;
    12.          _month = month;
    13.          _day = day;
    14.     }
    15.  private:
    16.      int _year;
    17.      int _month;
    18.      int _day;
    19. };
    20.  
    21.  void TestDate()
    22. {
    23.      Date d1; // 调用无参构造函数
    24.      Date d2(2015, 1, 1); // 调用带参的构造函数
    25.  
    26.      // 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
    27.      // 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
    28.      // warning C4930: “Date d3(void)”: 未调用原型函数(是否是有意用变量定义的?)
    29.      Date d3();
    30. }
    1.  class Date
    2. {
    3.  public:
    4. /*
    5. // 如果用户显式定义了构造函数,编译器将不再生成
    6. Date(int year, int month, int day)
    7. {
    8. _year = year;
    9. _month = month;
    10. _day = day;
    11. }
    12. */
    13. void Print()
    14. {
    15. cout << _year << "-" << _month << "-" << _day << endl;
    16. }
    17.  
    18.  private:
    19. int _year;
    20. int _month;
    21. int _day;
    22. };
    23.  
    24.  int main()
    25. {
    26. // 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函
    27. // 将Date类中构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再
    28. 生成
    29.      // 无参构造函数,放开后报错:error C2512: “Date”: 没有合适的默认构造函数可用
    30. Date d1;
    31. return 0;
    32. }
    关于编译器生成的默认成员函数,很多人会有疑惑:不实现构造函数的情况下,编译器会
    生成默认的构造函数。但是看起来默认构造函数又没什么用? d 对象调用了编译器生成的默
    认构造函数,但是 d 对象 _year/_month/_day ,依旧是随机值。也就说在这里 编译器生成的
    默认构造函数并没有什么用??
    解答: C++ 把类型分成内置类型 ( 基本类型 ) 和自定义类型。内置类型就是语言提供的数据类
    型,如: int/char... ,自定义类型就是我们使用 class/struct/union 等自己定义的类型,看看
    下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员 _t 调用的它的默认成员
    函数。 (需要注意的是,该种情况下,需要自定义成员变量的构造函数是默认构造函数,如果该自定义成员变量无默认构造函数,则编译器报错)
    1. class Time
    2. {
    3. public:
    4. Time()
    5. {
    6. cout << "Time()" << endl;
    7. _hour = 0;
    8. _minute = 0;
    9. _second = 0;
    10. }
    11. private:
    12. int _hour;
    13. int _minute;
    14. int _second;
    15. };
    16. class Date
    17. {
    18. private:
    19. // 基本类型(内置类型)
    20. int _year;
    21. int _month;
    22. int _day;
    23. // 自定义类型
    24. Time _t;
    25. };
    26. int main()
    27. {
    28. Date d;
    29. return 0;
    30. }
    注意: C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即: 内置类型成员变量在
    类中声明时可以给默认值
    1. class Time
    2. {
    3. public:
    4. Time()
    5. {
    6. cout << "Time()" << endl;
    7. _hour = 0;
    8. _minute = 0;
    9. _second = 0;
    10. }
    11. private:
    12. int _hour;
    13. int _minute;
    14. int _second;
    15. };
    16. class Date
    17. {
    18. private:
    19. // 基本类型(内置类型)
    20. int _year = 1970;
    21. int _month = 1;
    22. int _day = 1;
    23. // 自定义类型
    24. Time _t;
    25. };
    26. int main()
    27. {
    28. Date d;
    29. return 0;
    30. }
    无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。
    注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为
    是默认构造函数。

    3.析构函数

    3.1 概念

    通过前面构造函数的学习,我们知道一个对象是怎么来的,那一个对象又是怎么没呢的?
    析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由
    编译器完成的。而 对象在销毁时会自动调用析构函数,完成对象中资源的清理工作

    3.2 特性

    析构函数 是特殊的成员函数,其 特征 如下:
    • 1. 析构函数名是在类名前加上字符 ~
    • 2. 无参数无返回值类型。
    • 3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构
    • 函数不能重载
    • 4. 对象生命周期结束时,C++编译系统系统自动调用析构函数
    1. typedef int DataType;
    2. class Stack
    3. {
    4. public:
    5. Stack(size_t capacity = 3)
    6. {
    7. _array = (DataType*)malloc(sizeof(DataType) * capacity);
    8. if (NULL == _array)
    9. {
    10. perror("malloc申请空间失败!!!");
    11. return;
    12. }
    13. _capacity = capacity;
    14. _size = 0;
    15. }
    16. void Push(DataType data)
    17. {
    18. // CheckCapacity();
    19. _array[_size] = data;
    20. _size++;
    21. }
    22. // 其他方法...
    23. ~Stack()
    24. {
    25. if (_array)
    26. {
    27. free(_array);
    28. _array = NULL;
    29. _capacity = 0;
    30. _size = 0;
    31. }
    32. }
    33. private:
    34. DataType* _array;
    35. int _capacity;
    36. int _size;
    37. };
    38. void TestStack()
    39. {
    40. Stack s;
    41. s.Push(1);
    42. s.Push(2);
    43. }

    关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器

    生成的默认析构函数,对自定类型成员调用它的析构函数。
    1. class Time
    2. {
    3. public:
    4. ~Time()
    5. {
    6. cout << "~Time()" << endl;
    7. }
    8. private:
    9. int _hour;
    10. int _minute;
    11. int _second;
    12. };
    13. class Date
    14. {
    15. private:
    16. // 基本类型(内置类型)
    17. int _year = 1970;
    18. int _month = 1;
    19. int _day = 1;
    20. // 自定义类型
    21. Time _t;
    22. };
    23. int main()
    24. {
    25. Date d;
    26. return 0;
    27. }
    28. // 程序运行结束后输出:~Time()
    29. // 在main方法中根本没有直接创建Time类的对象,为什么最后会调用Time类的析构函数?
    30. // 因为:main方法中创建了Date对象d,而d中包含4个成员变量,其中_year, _month,
    31. _day三个是
    32. // 内置类型成员,销毁时不需要资源清理,最后系统直接将其内存回收即可;而_t是Time类对
    33. 象,所以在
    34. // d销毁时,要将其内部包含的Time类的_t对象销毁,所以要调用Time类的析构函数。但是:
    35. main函数
    36. // 中不能直接调用Time类的析构函数,实际要释放的是Date类对象,所以编译器会调用Date
    37. 类的析构函
    38. // 数,而Date没有显式提供,则编译器会给Date类生成一个默认的析构函数,目的是在其内部
    39. 调用Time
    40. // 类的析构函数,即当Date对象销毁时,要保证其内部每个自定义对象都可以正确销毁
    41. // main函数中并没有直接调用Time类析构函数,而是显式调用编译器为Date类生成的默认析
    42. 构函数
    43. // 注意:创建哪个类的对象则调用该类的析构函数,销毁那个类的对象则调用该类的析构函数
    如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如
    Date 类;有资源申请时,一定要写,否则会造成资源泄漏,比如 Stack 类(有malloc开辟的空间)。

    4. 拷贝构造函数

    4.1 概念

    拷贝构造函数 只有单个形参 ,该形参是对本 类类型对象的引用 ( 一般常用 const 修饰 ) ,在用 已存
    在的类类型对象创建新对象时由编译器自动调用

    4.2 特征

    拷贝构造函数也是特殊的成员函数,其 特征 如下:
    • 1. 拷贝构造函数是构造函数的一个重载形式
    • 2. 拷贝构造函数的参数只有一个必须是类类型对象的引用,使用传值方式编译器直接报错
    • 因为会引发无穷递归调用。(因为传值传参,会拷贝一份实参,然后传给形参)
    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. // Date(const Date& d)   // 正确写法
    11.    Date(const Date& d)   // 错误写法:编译报错,会引发无穷递归
    12. {
    13. _year = d._year;
    14. _month = d._month;
    15. _day = d._day;
    16. }
    17. private:
    18. int _year;
    19. int _month;
    20. int _day;
    21. };
    22. int main()
    23. {
    24. Date d1;
    25. Date d2(d1);
    26. return 0;
    27. }

     

    若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按
    字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
    1. class Time
    2. {
    3. public:
    4. Time()
    5. {
    6. _hour = 1;
    7. _minute = 1;
    8. _second = 1;
    9. }
    10. Time(const Time& t)
    11. {
    12. _hour = t._hour;
    13. _minute = t._minute;
    14. _second = t._second;
    15. cout << "Time::Time(const Time&)" << endl;
    16. }
    17. private:
    18. int _hour;
    19. int _minute;
    20. int _second;
    21. };
    22. class Date
    23. {
    24. private:
    25. // 基本类型(内置类型)
    26. int _year = 1970;
    27. int _month = 1;
    28. int _day = 1;
    29. // 自定义类型
    30. Time _t;
    31. };
    32. int main()
    33. {
    34. Date d1;
    35.    
    36.    // 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
    37.    // 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构
    38. 造函数
    39. Date d2(d1);
    40. return 0;
    41. }
    注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定
    义类型是调用其拷贝构造函数完成拷贝的。
    编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了 ,还需要自己显式实现吗?
    当然像日期类这样的类是没必要的。那么下面的类呢?
    1. // 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
    2. typedef int DataType;
    3. class Stack
    4. {
    5. public:
    6. Stack(size_t capacity = 10)
    7. {
    8. _array = (DataType*)malloc(capacity * sizeof(DataType));
    9. if (nullptr == _array)
    10. {
    11. perror("malloc申请空间失败");
    12. return;
    13. }
    14. _size = 0;
    15. _capacity = capacity;
    16. }
    17. void Push(const DataType& data)
    18. {
    19. // CheckCapacity();
    20. _array[_size] = data;
    21. _size++;
    22. }
    23. ~Stack()
    24. {
    25. if (_array)
    26. {
    27. free(_array);
    28. _array = nullptr;
    29. _capacity = 0;
    30. _size = 0;
    31. }
    32. }
    33. private:
    34. DataType *_array;
    35. size_t _size;
    36. size_t _capacity;
    37. };
    38. int main()
    39. {
    40. Stack s1;
    41. s1.Push(1);
    42. s1.Push(2);
    43. s1.Push(3);
    44. s1.Push(4);
    45. Stack s2(s1);
    46. return 0;
    47. }

    注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请
    时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
    拷贝构造函数典型调用场景:
    • 使用已存在对象创建新对象
    • 函数参数类型为类类型对象
    • 函数返回值类型为类类型对象
    1. class Date
    2. {
    3. public:
    4. Date(int year, int minute, int day)
    5. {
    6. cout << "Date(int,int,int):" << this << endl;
    7. }
    8. Date(const Date& d)
    9. {
    10. cout << "Date(const Date& d):" << this << endl;
    11. }
    12. ~Date()
    13. {
    14. cout << "~Date():" << this << endl;
    15. }
    16. private:
    17. int _year;
    18. int _month;
    19. int _day;
    20. };
    21. Date Test(Date d)
    22. {
    23. Date temp(d);
    24. return temp;
    25. }
    26. int main()
    27. {
    28. Date d1(2022,1,13);
    29. Test(d1);
    30. return 0;
    31. }
    为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用
    尽量使用引用。

     

    5.赋值运算符重载

    5.1 运算符重载

    C++ 为了增强代码的可读性引入了运算符重载 运算符重载是具有特殊函数名的函数 ,也具有其
    返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
    函数名字为:关键字 operator 后面接需要重载的运算符符号
    函数原型: 返回值类型  operator 操作符 ( 参数列表 )
    注意:
    • 不能通过连接其他符号来创建新的操作符:比如operator@
    • 重载操作符必须有一个类类型参数
    • 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
    • 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐
    • 藏的this
    • .* :: sizeof ?: . 注意以上5个运算符不能重载
    1. // 全局的operator==
    2. class Date
    3. {
    4. public:
    5. Date(int year = 1900, int month = 1, int day = 1)
    6.   {
    7.        _year = year;
    8.        _month = month;
    9.        _day = day;
    10.   }    
    11. //private:
    12. int _year;
    13. int _month;
    14. int _day;
    15. };
    16. // 这里会发现运算符重载成全局的就需要成员变量是公有的,那么问题来了,封装性如何保证?
    17. // 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
    18. bool operator==(const Date& d1, const Date& d2)
    19. {
    20.    return d1._year == d2._year
    21.   && d1._month == d2._month
    22.        && d1._day == d2._day;
    23. }
    24. void Test ()
    25. {
    26.    Date d1(2018, 9, 26);
    27.    Date d2(2018, 9, 27);
    28.    cout<<(d1 == d2)<
    29. }
    30. class Date
    31. {
    32. public:
    33. Date(int year = 1900, int month = 1, int day = 1)
    34.   {
    35.        _year = year;
    36.        _month = month;
    37.        _day = day;
    38.   }
    39.    
    40.    // bool operator==(Date* this, const Date& d2)
    41.    // 这里需要注意的是,左操作数是this,指向调用函数的对象
    42.    bool operator==(const Date& d2)
    43. {
    44.        return _year == d2._year;
    45.            && _month == d2._month
    46.            && _day == d2._day;
    47. }
    48. private:
    49. int _year;
    50. int _month;
    51. int _day;
    52. };

    5.2 赋值运算符重载

    1. 赋值运算符重载格式
    • 参数类型const T&,传递引用可以提高传参效率
    • 返回值类型T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
    • 检测是否自己给自己赋值
    • 返回*this :要复合连续赋值的含义
    1. class Date
    2. {
    3. public :
    4. Date(int year = 1900, int month = 1, int day = 1)
    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. Date& operator=(const Date& d)
    17. {
    18. if(this != &d)
    19.       {
    20.            _year = d._year;
    21.            _month = d._month;
    22.            _day = d._day;
    23.       }
    24.        
    25.        return *this;
    26. }
    27. private:
    28. int _year ;
    29. int _month ;
    30. int _day ;
    31. };
    2. 赋值运算符只能重载成类的成员函数不能重载成全局函数
    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. int _year;
    11. int _month;
    12. int _day;
    13. };
    14. // 赋值运算符重载成全局函数,注意重载成全局函数时没有this指针了,需要给两个参数
    15. Date& operator=(Date& left, const Date& right)
    16. {
    17. if (&left != &right)
    18. {
    19. left._year = right._year;
    20. left._month = right._month;
    21. left._day = right._day;
    22. }
    23. return left;
    24. }
    25. // 编译失败:
    26. // error C2801: “operator =”必须是非静态成员

    因为赋值重载是类的六大默认成员函数之一,如果不显示在类中声明(定义),编译器就会自动生成,此时就会与定义在全局的赋值重载函数发生冲突

    3. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝 。注
    意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符
    重载完成赋值。
    1. class Time
    2. {
    3. public:
    4. Time()
    5. {
    6. _hour = 1;
    7. _minute = 1;
    8. _second = 1;
    9. }
    10. Time& operator=(const Time& t)
    11. {
    12. if (this != &t)
    13. {
    14. _hour = t._hour;
    15. _minute = t._minute;
    16. _second = t._second;
    17. }
    18. return *this;
    19. }
    20. private:
    21. int _hour;
    22. int _minute;
    23. int _second;
    24. };
    25. class Date
    26. {
    27. private:
    28. // 基本类型(内置类型)
    29. int _year = 1970;
    30. int _month = 1;
    31. int _day = 1;
    32. // 自定义类型
    33. Time _t;
    34. };
    35. int main()
    36. {
    37. Date d1;
    38. Date d2;
    39. d1 = d2;
    40. return 0;
    41. }
    既然 编译器生成的默认赋值运算符重载函数已经可以完成字节序的值拷贝了 ,还需要自己实
    现吗?当然像日期类这样的类是没必要的。那么下面的类呢?
    1. // 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
    2. typedef int DataType;
    3. class Stack
    4. {
    5. public:
    6. Stack(size_t capacity = 10)
    7. {
    8. _array = (DataType*)malloc(capacity * sizeof(DataType));
    9. if (nullptr == _array)
    10. {
    11. perror("malloc申请空间失败");
    12. return;
    13. }
    14. _size = 0;
    15. _capacity = capacity;
    16. }
    17. void Push(const DataType& data)
    18. {
    19. // CheckCapacity();
    20. _array[_size] = data;
    21. _size++;
    22. }
    23. ~Stack()
    24. {
    25. if (_array)
    26. {
    27. free(_array);
    28. _array = nullptr;
    29. _capacity = 0;
    30. _size = 0;
    31. }
    32. }
    33. private:
    34. DataType *_array;
    35. size_t _size;
    36. size_t _capacity;
    37. };
    38. int main()
    39. {
    40. Stack s1;
    41. s1.Push(1);
    42. s1.Push(2);
    43. s1.Push(3);
    44. s1.Push(4);
    45. Stack s2;
    46. s2 = s1;
    47. return 0;
    48. }

     注意:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必

    须要实现。

    5.3 前置++和后置++重载

    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. // 前置++:返回+1之后的结果
    11. // 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
    12. Date& operator++()
    13. {
    14. _day += 1;
    15. return *this;
    16. }
    17. // 后置++:
    18. // 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
    19. // C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器
    20. 自动传递
    21. // 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存
    22. 一份,然后给this+1
    23. //       而temp是临时对象,因此只能以值的方式返回,不能返回引用
    24. Date operator++(int)
    25. {
    26. Date temp(*this);
    27. _day += 1;
    28. return temp;
    29. }
    30. private:
    31. int _year;
    32. int _month;
    33. int _day;
    34. };
    35. int main()
    36. {
    37. Date d;
    38. Date d1(2022, 1, 13);
    39. d = d1++;    // d: 2022,1,13   d1:2022,1,14
    40. d = ++d1;    // d: 2022,1,15   d1:2022,1,15
    41. return 0;
    42. }

    7.const成员

    const 修饰的 成员函数 称之为 const 成员函数 const 修饰类成员函数,实际修饰该成员函数
    的*this,表明在该成员函数中 不能对类的任何成员进行修改。
    1. class Date
    2. {
    3. public:
    4. Date(int year, int month, int day)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. void Print()
    11. {
    12. cout << "Print()" << endl;
    13. cout << "year:" << _year << endl;
    14. cout << "month:" << _month << endl;
    15. cout << "day:" << _day << endl << endl;
    16. }
    17. void Print() const
    18. {
    19. cout << "Print()const" << endl;
    20. cout << "year:" << _year << endl;
    21. cout << "month:" << _month << endl;
    22. cout << "day:" << _day << endl << endl;
    23. }
    24. private:
    25. int _year; // 年
    26. int _month; // 月
    27. int _day; // 日
    28. };
    29. void Test()
    30. {
    31. Date d1(2022,1,13);
    32. d1.Print();
    33. const Date d2(2022,1,13);
    34. d2.Print();
    35. }
    请思考下面的几个问题:
    1. const 对象可以调用非 const 成员函数吗?
    肯定是不可以的,一个只读的对象怎么可以用一个可读可写的*this接收呢
    2. const 对象可以调用 const 成员函数吗?
    这个是可以的,从可读可写到只读是权限的缩小
    3. const 成员函数内可以调用其它的非 const 成员函数吗?
    不可以, const成员函数内部只能调用const成员函数。因为const成员函数内部的this指针已经具有常属性的,万一这个非const成员函数去修改了 成员变量的内容就会出问题了
    4. const 成员函数内可以调用其它的 const 成员函数吗?
    肯定是可以的啦

    8.取地址及const取地址操作符重载

    这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
    1. class Date
    2. {
    3. public :
    4. Date* operator&()
    5. {
    6. return this ;
    7. }
    8. const Date* operator&()const
    9. {
    10. return this ;
    11. }
    12. private :
    13. int _year ; // 年
    14. int _month ; // 月
    15. int _day ; // 日
    16. };

  • 相关阅读:
    SpringBoot-AOP学习案例
    VR全景算不算好的创业项目?有哪些特性?
    MySQL覆盖索引的使用
    腾讯、百度花式布局CRM
    数据结构——二叉搜索树
    QT当中的connect+eventFilter函数全解
    使用nohup命令 或者 代码创建守护进程
    算法-煤球数目
    libopus 实现pcm 编码到opus
    win11无法打开chm格式的文件
  • 原文地址:https://blog.csdn.net/lmy050813/article/details/138089014