L.L.569 2021-12-28 22:05 采纳率: 100%
浏览 34
已结题

不懂main函数中的第二个for循环的意思

问题遇到的现象和发生背景

(time++).show();表示什么意思,为什么returne 0;上一行又有一个show函数

问题相关代码,请勿粘贴截图
#include<iostream>
using namespace std;
class Time {
        int hour;
        int minute;
        int second;
    public:
        Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s) {
            cout<<"构造函数!"<<endl;
        }
        Time operator++();
        Time operator++(int);
        void show() {
            cout<<hour<<":"<<minute<<":"<<second<<endl;
        }
        ~Time() {
            cout<<"析构函数!"<<endl;
        }
};
Time Time::operator ++() {
    second++;
    if(second>=60) {
        second-=60;
        minute++;
        if(minute>=60) {
            {
                minute-=60;
                hour++;
            }
            if(hour>=24) {
                hour=0;
            }
        }
    }
    return *this;
}
Time Time::operator++(int) {
    Time old=*this;
    second++;
    if(second>=60) {
        second-=60;
        minute++;
        if(minute>=60) {
            {
                minute-=60;
                hour++;
            }
            if(hour>=24) {
                hour=0;
            }
        }
    }
    return old;
}
int main() {
    Time time(11,59,58);
    int i;
    for(i=0; i<3; i++) {
        ++time;
        time.show();
    }
    for(i=0; i<3; i++) {
        (time++).show();
    }
    time.show();
    return 0;
}

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • _GX_ 2021-12-28 22:22
    关注

    (time++).show();表示调用Time::operator++(int)函数,然后对返回值调用Time::show()
    return 0;main()函数返回值,一般0表示没有错误
    最后一个time.show()显示的是最后一次后缀++的结果

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月29日
  • 已采纳回答 12月29日
  • 创建了问题 12月28日

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效