程序的输出为
with no const
with const
12
16
请问为什么不是字符串和数字一组一组输出,而且不先调用常成员函数?
程序如下:
#include
using namespace std;
class A
{
private:
int w,h;
public:
int getValue() const;
int getValue();
A(int x,int y)
{
w=x;h=y;
}
A(){}
};
int A::getValue()
{
cout<<"with no const"<<endl;
return w*h;
}
int A::getValue() const
{
cout<<"with const"<<endl;
return w*h;
}
int main()
{
A const a(3,4);
A c(2,8);
cout<<a.getValue()<<endl<<c.getValue()<<endl;
}