任何人都可以帮助我理解 - >重载以访问类中的数据成员(c ++)

时间:2015-10-07 12:37:28

标签: c++

我需要一个简单的例子。我知道+, - ,*,<<,>>等运算符的基本重载请帮助完成我的代码。

#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;
}

1 个答案:

答案 0 :(得分:0)

由于xy是私有的,因此您无法使代码正常工作。

如果你将它们公开,这将有效:

XY* operator ->() 
{
    return this;
}