• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C++类的默认成员函数 —— 赋值重载

武飞扬头像
Wihkum
帮助1

、概念
C 为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类 型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
注意:
1、 不能通过连接其他符号来创建新的操作符:比如operator@
2、 重载操作符必须有一个类类型参数用于内置类型的运算符,其含义不能改变,例如:内置的整型 ,不能改变其含义
3、 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
4、 .*   ::   sizeof   ?:   .    注意以上5个运算符不能重。
二、赋值运算符重载格式
参数类型:const T&,传递引用可以提高传参效率
返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值检测是否自己给自己赋值
返回*this :要复合连续赋值的含义 
赋值运算符只能重载成类的成员函数不能重载成全局函数
原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。
四、 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。
下面写一个完整的日期类来实现赋值运算符的重载:
Date.h 文件 
  1.  
    #pragma once
  2.  
    #include <assert.h>
  3.  
     
  4.  
    #include <iostream>
  5.  
    using namespace std;
  6.  
     
  7.  
    class Date
  8.  
    {
  9.  
     
  10.  
    // 友元函数 ==> 这个函数内部可以使用 Date 对象访问私有保护成员
  11.  
    //friend void operator<<(ostream& out, const Date& d); // 只能输出一个 cout << d1 不支持连续输出 cout << d1 << d2;
  12.  
    friend ostream& operator<<(ostream& out, const Date& d); // 只能输出一个 cout << d1 不支持连续输出 cout << d1 << d2;
  13.  
    friend istream& operator>>(istream& out, Date& d);
  14.  
     
  15.  
    public:
  16.  
    // 构造函数频繁调用,所以直接放在类里面定义作为 inline
  17.  
    Date(int year = 1, int month = 1, int day = 1)
  18.  
    {
  19.  
    _year = year;
  20.  
    _month = month;
  21.  
    _day = day;
  22.  
     
  23.  
    if (!CheckDate())
  24.  
    {
  25.  
    Print();
  26.  
    cout << "初始日期错误" << endl;
  27.  
    }
  28.  
    }
  29.  
     
  30.  
    bool operator == (const Date& d) const;
  31.  
    bool operator > (const Date& d) const;
  32.  
    bool operator < (const Date& d) const;
  33.  
    bool operator >= (const Date& d) const;
  34.  
    bool operator <= (const Date& d) const;
  35.  
    bool operator != (const Date& d) const;
  36.  
     
  37.  
    int GetMonthDay(int year, int month)
  38.  
    {
  39.  
    // 加 static 为了防止多次频繁调用函数之后多次频繁创建数组,加 static 数组在静态区上创建 每次访问都是这一个
  40.  
    static int Month_Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  41.  
    if (month == 2
  42.  
    && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
  43.  
    {
  44.  
    return 29;
  45.  
    }
  46.  
    else
  47.  
    {
  48.  
    return Month_Day[month];
  49.  
    }
  50.  
    }
  51.  
     
  52.  
    // 检查最开始就把日期写错
  53.  
    bool CheckDate()
  54.  
    {
  55.  
    if (_year >= 1
  56.  
    && _month >= 1 && _month <= 12
  57.  
    && _day >= 1 && _day <= GetMonthDay(_year, _month))
  58.  
    {
  59.  
    return true;
  60.  
    }
  61.  
    else
  62.  
    {
  63.  
    return false;
  64.  
    }
  65.  
    }
  66.  
     
  67.  
    //void Print();
  68.  
    void Print() const;
  69.  
     
  70.  
    // d1 day
  71.  
    Date operator (int day) const;
  72.  
    Date& operator = (int day);
  73.  
     
  74.  
    // d1 后置 ,返回 之后的
  75.  
    // d1 前置 ,返回 之前的
  76.  
    // 直接按特性重载,无法区分
  77.  
    // 特殊处理,使用重载区分 后置 重载增加一个 int 参数跟前置构成函数重载进行区分
  78.  
    // Date operator (); // 这样的话没办法区分写的是前置还是后置
  79.  
     
  80.  
    Date& operator (); // 前置
  81.  
    Date operator (int); // 后置 加不加形参接收不接收都可以 但是规定类型是 int
  82.  
     
  83.  
     
  84.  
    // d1 - day
  85.  
    Date operator - (int day) const;
  86.  
    Date& operator -= (int day);
  87.  
     
  88.  
    Date& operator--();
  89.  
    Date operator--(int);
  90.  
     
  91.  
    // d2 - d1 ==> 日期 - 日期 = 天数
  92.  
    int operator - (Date& d) const;
  93.  
     
  94.  
    // 输出流 << 操作符 <==> 操作符可以接收内置类型的 无法接受自定义类型
  95.  
    // void operator<<(ostream& out); // 默认第一个参数是 this (放这里不太合适)
  96.  
     
  97.  
    private:
  98.  
    int _year;
  99.  
    int _month;
  100.  
    int _day;
  101.  
    };
  102.  
     
  103.  
    // 流插入重载
  104.  
    //
  105.  
    // void operator<<(ostream& out, const Date& d); // 放在类外面 将参数的位置可以改变
  106.  
    // 会被频繁调用 输出多行 应该加上 inline
  107.  
    // ostream& operator<<(ostream& out, const Date& d); // 放在类外面 将参数的位置可以改变
  108.  
    // 加上 inline 又链接不上,内联 不进 符号表 链接找不到 ===> 所以 定义 和 声明 不要分离
  109.  
    inline ostream& operator<<(ostream& out, const Date& d)
  110.  
    {
  111.  
    // 因为函数在类外面定义的 所以无法访问 private 的成员
  112.  
    // 解决办法 1. 用 get_year get_month get_day 2. 把 private 的成员放开 public (不建议) 3. 友元函数
  113.  
    out << d._year << "-" << d._month << "-" << d._day << endl;
  114.  
    return out;
  115.  
    }
  116.  
     
  117.  
    // 流提取重载
  118.  
    inline istream& operator>>(istream& in, Date& d)
  119.  
    {
  120.  
    in >> d._year >> d._month >> d._day;
  121.  
     
  122.  
    assert(d.CheckDate());
  123.  
     
  124.  
    return in;
  125.  
     
  126.  
    }
学新通

Date.cpp 文件

  1.  
    #define _CRT_SECURE_NO_WARNINGS 1
  2.  
     
  3.  
    #include "Date.h"
  4.  
     
  5.  
    // 任何一个类要重载运算符只需要写一个 >和== 或者 <和== 重载, 剩下比较运算符复用即可
  6.  
     
  7.  
    // d1 == d2
  8.  
    bool Date::operator == (const Date& d) const
  9.  
    {
  10.  
    return _year == d._year
  11.  
    && _month == d._month
  12.  
    && _day == d._day;
  13.  
    }
  14.  
     
  15.  
    // d1 > d2
  16.  
    bool Date::operator > (const Date& d) const
  17.  
    {
  18.  
    if ((_year > d._year)
  19.  
    || (_year == d._year && _month > d._month)
  20.  
    || (_year == d._year && _month == d._month && _day > d._day))
  21.  
    {
  22.  
    return true;
  23.  
    }
  24.  
    else
  25.  
    {
  26.  
    return false;
  27.  
    }
  28.  
    }
  29.  
     
  30.  
    // d1 < d2
  31.  
    bool Date::operator < (const Date& d) const// Date* const this, const Date
  32.  
    {
  33.  
    return !(*this >= d);
  34.  
    }
  35.  
     
  36.  
    // d1 >= d2
  37.  
    bool Date::operator >= (const Date& d) const
  38.  
    {
  39.  
    return (*this > d) || (*this == d);
  40.  
    }
  41.  
     
  42.  
    // d1 <= d2
  43.  
    bool Date::operator <= (const Date& d) const
  44.  
    {
  45.  
    return !(*this > d);
  46.  
    }
  47.  
     
  48.  
    // d1 != d2
  49.  
    bool Date::operator != (const Date& d) const
  50.  
    {
  51.  
    return !(*this == d); // 逻辑取反
  52.  
    }
  53.  
     
  54.  
     
  55.  
    // 先写 = 再复用到 (调用两次拷贝)
  56.  
    // d1 day
  57.  
    Date Date::operator (int day) const // 不加引用;因为 ret 出了 Date 作用域就销毁了
  58.  
    {
  59.  
    Date ret(*this); // 拷贝构造
  60.  
     
  61.  
    ret = day;
  62.  
     
  63.  
    return ret; // 返回 ret 然后会拷贝构造
  64.  
    }
  65.  
     
  66.  
    // d1 = day
  67.  
    Date& Date::operator = (int day)
  68.  
    {
  69.  
    if (day < 0)
  70.  
    {
  71.  
    return *this -= -day;
  72.  
    }
  73.  
     
  74.  
    _day = day;
  75.  
    while (_day > GetMonthDay(_year, _month))
  76.  
    {
  77.  
    _day -= GetMonthDay(_year, _month);
  78.  
    _month ;
  79.  
    if (_month == 13)
  80.  
    {
  81.  
    _year ;
  82.  
    _month = 1;
  83.  
    }
  84.  
    }
  85.  
    return *this;
  86.  
    }
  87.  
     
  88.  
    // 如果先写 再复用到 = (会调用多次拷贝效率低)
  89.  
    //Date Date::operator (int day) // 不加引用;因为 ret 出了 Date 作用域就销毁了
  90.  
    //{
  91.  
    // Date ret(*this); // 拷贝构造
  92.  
    //
  93.  
    // ret._day = day;
  94.  
    // while (ret._day > GetMonthDay(ret._year, ret._month))
  95.  
    // {
  96.  
    // ret._day -= GetMonthDay(ret._year, ret._month);
  97.  
    // ret._month ;
  98.  
    // if (ret._month == 13)
  99.  
    // {
  100.  
    // ret._year ;
  101.  
    // ret._month = 1;
  102.  
    // }
  103.  
    // }
  104.  
    //
  105.  
    // return ret; // 返回 ret 然后会拷贝构造
  106.  
    //}
  107.  
    //
  108.  
    d1 = day
  109.  
    //Date& Date::operator = (int day)
  110.  
    //{
  111.  
    // *this = *this day;
  112.  
    //
  113.  
    // return *this;
  114.  
    //}
  115.  
     
  116.  
    Date& Date::operator () // 前置
  117.  
    {
  118.  
    //*this = 1;
  119.  
    //return *this;
  120.  
     
  121.  
    return *this = 1; // 返回值没有销毁 用引用返回
  122.  
    }
  123.  
     
  124.  
    Date Date::operator (int) // 后置
  125.  
    {
  126.  
    Date temp(*this);
  127.  
    *this = 1;
  128.  
     
  129.  
    return temp;
  130.  
    }
  131.  
     
  132.  
    Date Date::operator-(int day) const
  133.  
    {
  134.  
    Date temp(
  135.  
    *this);
  136.  
    temp -= day;
  137.  
    return temp;
  138.  
    }
  139.  
     
  140.  
    Date& Date::operator-=(int day)
  141.  
    {
  142.  
    if (day < 0)
  143.  
    {
  144.  
    return *this = -day;
  145.  
    }
  146.  
     
  147.  
    _day -= day;
  148.  
    while (_day <= 0)
  149.  
    {
  150.  
    --_month;
  151.  
    if (_month == 0)
  152.  
    {
  153.  
    _year--;
  154.  
    _month = 12;
  155.  
    }
  156.  
     
  157.  
    _day = GetMonthDay(_year, _month);
  158.  
    }
  159.  
     
  160.  
    return *this;
  161.  
    }
  162.  
     
  163.  
    Date& Date::operator--()
  164.  
    {
  165.  
    return *this -= 1;
  166.  
    }
  167.  
     
  168.  
    Date Date::operator--(int)
  169.  
    {
  170.  
    Date temp(*this);
  171.  
    *this -= 1;
  172.  
    return temp;
  173.  
    }
  174.  
     
  175.  
    // d2 - d1 ==> 日期 - 日期 = 天数
  176.  
    int Date::operator - (Date& d) const
  177.  
    {
  178.  
    int flag = 1;
  179.  
    Date max = *this;
  180.  
    Date min = d;
  181.  
     
  182.  
    if (max < min)
  183.  
    {
  184.  
    max = d;
  185.  
    min = *this;
  186.  
    flag = -1;
  187.  
    }
  188.  
     
  189.  
    int n = 0;
  190.  
    while (min != max)
  191.  
    {
  192.  
    min ;
  193.  
    n ;
  194.  
    }
  195.  
     
  196.  
    return n * flag;
  197.  
    }
  198.  
     
  199.  
    流操作符 <<
  200.  
    //void Date::operator<<(ostream& out) // 写在类里面的
  201.  
    //{
  202.  
    // out << _year << "-" << _month << "-" << _day << endl;
  203.  
    //}
  204.  
    //void operator<<(ostream& out, const Date& d) // 放在类外面 将参数的位置可以改变
  205.  
    //ostream& operator<<(ostream& out, const Date& d)
  206.  
    //{
  207.  
    // // 因为函数在类外面定义的 所以无法访问 private 的成员
  208.  
    // // 解决办法 1. 用 get_year get_month get_day 2. 把 private 的成员放开 public (不建议) 3. 友元函数
  209.  
    // out << d._year << "-" << d._month << "-" << d._day << endl;
  210.  
    // return out;
  211.  
    //}
  212.  
     
  213.  
    //void Date::Print() // const Date* const this
  214.  
    void Date::Print() const // const Date*
  215.  
    {
  216.  
    cout << _year << "-" << _month << "-" << _day << endl;
  217.  
    }
学新通

TestDate.cpp 文件

  1.  
    //void TestDate1()
  2.  
    //{
  3.  
    // Date d1(2022, 11, 8);
  4.  
    //
  5.  
    // (d1 4).Print(); // 跨天
  6.  
    // (d1 40).Print(); // 跨月
  7.  
    // (d1 400).Print(); // 跨年
  8.  
    // (d1 4000).Print();// 跨闰年
  9.  
    //
  10.  
    // Date ret1 = d1; // d1.operator (&d1)
  11.  
    // Date ret2 = d1 ; // d1.operator (&d1, 0) 整数传什么都可以 只是为了构成重载进行区分
  12.  
    //}
  13.  
     
  14.  
    void TestDate2()
  15.  
    {
  16.  
    Date d1(2022, 11, 14);
  17.  
     
  18.  
    //(d1 - 4).Print(); // 跨天
  19.  
    //(d1 - 40).Print(); // 跨月
  20.  
    //(d1 - 400).Print(); // 跨年
  21.  
    //(d1 - 4000).Print();// 跨闰年
  22.  
     
  23.  
    //Date ret1 = d1; // d1.operator (&d1)
  24.  
    //Date ret2 = d1 ; // d1.operator (&d1, 0) 整数传什么都可以 只是为了构成重载进行区分
  25.  
     
  26.  
    Date d2(2000, 5, 1);
  27.  
    Date d3(2022, 11, 14);
  28.  
     
  29.  
    cout << d3 - d2 << endl;
  30.  
    cout << d2 - d3 << endl;
  31.  
     
  32.  
    }
  33.  
     
  34.  
    void TestDate3()
  35.  
    {
  36.  
    // 初始日期错误
  37.  
    Date d1(2022, 11, 32);
  38.  
    Date d2(2022, 2, 29);
  39.  
    }
  40.  
     
  41.  
    void TestDate4()
  42.  
    {
  43.  
    Date d1(2022, 11, 20);
  44.  
     
  45.  
    // cout << d1; // 因为函数写在类里面 默认第一个参数是 this(d1) 所以应该是 d1 << cout
  46.  
    // d1.operator<<(cout);
  47.  
    // d1 << cout; // 但是这样写的话有与 cout << 不合适 ; 所以函数可以写在类外面将参数位置改变
  48.  
     
  49.  
    cout << d1;
  50.  
    Date d2(2000, 5, 1);
  51.  
    Date d3(2022, 11, 14);
  52.  
    cout << d1 << d2 << d3;
  53.  
     
  54.  
    cin >> d1 >> d2 >> d3;
  55.  
    cout << d1 << d2 << d3;
  56.  
     
  57.  
    }
  58.  
     
  59.  
    void menu()
  60.  
    {
  61.  
    cout << "***************************" << endl;
  62.  
    cout << "***** 1. 日期加/减天数 *****" << endl;
  63.  
    cout << "***** 2. 日期减日期 *****" << endl;
  64.  
    cout << "***** 3. 日期->周几 *****" << endl;
  65.  
    cout << "***** -1. 退出 *****" << endl;
  66.  
    cout << "***************************" << endl;
  67.  
    }
  68.  
     
  69.  
    void TestDate5()
  70.  
    {
  71.  
    const char* WeekDayToStr[] = { "周一", "周二", "周三", "周四", "周五", "周六", "周天" };
  72.  
     
  73.  
    Date d1, d2;
  74.  
    int day = 0;
  75.  
    int input = 0;
  76.  
     
  77.  
    do
  78.  
    {
  79.  
    menu();
  80.  
     
  81.  
    cout << "请选择:";
  82.  
    cin >> input;
  83.  
     
  84.  
    if (input == 1)
  85.  
    {
  86.  
    cout << "请输入日期及天数(减天数输入负数):";
  87.  
    cin >> d1 >> day;
  88.  
    cout << "日期加减天数后的日期为:" << d1 day << endl;
  89.  
    }
  90.  
    else if (input == 2)
  91.  
    {
  92.  
    cout << "请依次输入两个日期:";
  93.  
    cin >> d1 >> d2;
  94.  
    cout << "相差的天数:" << d1 - d2 << endl;
  95.  
    }
  96.  
    else if (input == 3)
  97.  
    {
  98.  
    cout << "请输入日期:";
  99.  
    cin >> d1;
  100.  
    Date start(1, 1, 1);
  101.  
    int n = d1 - start;
  102.  
    int WeekDay = 0; // 初始周一 初始周几存在不确定
  103.  
    //int WeekDay = 5; // 1 1 1 就是周六 但是中间删了 10 天
  104.  
    WeekDay = n;
  105.  
    //cout << "周" << WeekDay % 7 1 << endl; //
  106.  
    cout << WeekDayToStr[WeekDay % 7] << endl; //
  107.  
     
  108.  
     
  109.  
    }
  110.  
    else
  111.  
    {
  112.  
    cout << "输入错误,请重新输入:" << endl;
  113.  
    }
  114.  
     
  115.  
    } while (input != -1);
  116.  
     
  117.  
     
  118.  
    }
  119.  
     
  120.  
    void TestDate6()
  121.  
    {
  122.  
    // 只有 指针 和 引用 存在权限的放大和缩小
  123.  
     
  124.  
    Date d1(2022, 11, 23);
  125.  
    const Date d2(2022, 11, 24);
  126.  
     
  127.  
    d1.Print(); // &d1 Date*
  128.  
    //d2.Print(); // 权限放大,出错 &d2 const Date*
  129.  
    d2.Print();
  130.  
     
  131.  
     
  132.  
    // Date::operator < Date* const this, const Date
  133.  
    d1 < d2; // Date* < const Date*
  134.  
    //d2 < d1; // const Date* < Date* 出错,权限放大 所以要加const ==> Date::operator < const
  135.  
     
  136.  
    }
  137.  
     
  138.  
     
  139.  
    int main()
  140.  
    {
  141.  
    //TestDate1U
  142.  
    //TestDate2();
  143.  
    //TestDate3();
  144.  
    //TestDate4();
  145.  
    //TestDate5();
  146.  
    TestDate6();
  147.  
     
  148.  
    return 0;
  149.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggefkg
系列文章
更多 icon
同类精品
更多 icon
继续加载