我需要一个简单的例子。我知道+, - ,*,<<,>>等运算符的基本重载请帮助完成我的代码。
#include<iostream>
using namespace std;
class XY
{
int x,y;
public:
XY()
{
x=0;y=0;
}
friend ostream &operator<<( ostream &output,const XY &D )
{
output << "X : " << D.x << " Y: " << D.y;
return output;
}
friend istream &operator>>( istream &input,XY &D )
{
input >> D.x >> D.y;
return input;
}
- &GT;重载从这里开始
XY* operator ->()
{
}
};
int main()
{
XY a;
cout<<"Enter value of x & y "<<endl;
cin>>a;
cout<<a;
//way to access members
cout<<a;
}
答案 0 :(得分:0)
由于x
和y
是私有的,因此您无法使代码正常工作。
如果你将它们公开,这将有效:
XY* operator ->()
{
return this;
}