#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
为什么输出结果会变成这个样子呢?