这个输出结果是为啥呢?求助!!!

想吃腊肠 2023-10-28 21:25:39

#include <iostream>
using namespace std;

class A
{
private:
    int w,h;
public:
    int getValue() const;
    int getValue();
    A(int x,int y)
    {
        this->h=x;
        this->w=y;
    }
    A()
    {}
};

int A::getValue() const
{
    cout<<"with const"<<endl;
    return w*h;
}

int A::getValue()
{
    cout<<"with none const"<<endl;
    return w*h;
}

void main()
{
    A const a(3,4);
    A b(3,6);   
cout<<a.getValue()<<endl;
cout<<b.getValue()<<endl;
}
这个代码的输出结果为:
wi th const
12
with none const
18


但是将代码中的
cout<<a.getValue()<<endl;
cout<<b.getValue()<<endl;修改为
cout<<a.getValue()<<endl<<b.getValue()<<endl;
输出结果则变为:
with none const
with const
12
18

为什么输出结果会变成这个样子呢?

...全文
254 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

10

社区成员

发帖
与我相关
我的任务
社区描述
Docker
javascriptgolangdevops 个人社区
社区管理员
  • 甜点cc
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧