读书人

请问函数重载后的输出有关问题

发布时间: 2012-03-30 17:32:10 作者: rapoo

请教函数重载后的输出问题
#include <iostream.h>

class Point
{
public:
int x;
int y;

Point(int a,int b)
{
x=a;
y=b;
}
Point()
{
x=2;
y=2;
}
~Point()
{
}
void output()
{
cout < <x < <endl < <y < <endl;
}
void output(int x ,int y)
{
this-> x=x;
this-> y=y;
}
};

void main()
{
Point pt(3,3);
Point r;

pt.output(1,1);//*
pt.output();
r.output();
}

在上面这段程序中,我希望得到的结果是:
1
1
2
2
3
3
而实际得到的结果却是:
1
1
2
2
当注释掉带*的那行后,结果是:
3
3
2
2
请问是怎么一回事?就输出顺序来说,去掉带*那行后应该是:
2
2
3
3
如果想要正确输出应该怎么改。谢谢!

[解决办法]
void output(int x ,int y)const
{
cout < <x < <endl < <y < <endl;
}

这样就会有期望的输出了
[解决办法]
你的output(int x,int y)把原来的对象的指改变了,所以会有那样的输出.

读书人网 >C++

热点推荐