13,825
社区成员
发帖
与我相关
我的任务
分享
class X;
class Y{
friend std:: ostream & operator <<(Y &y,std::ostream &os) ;
public:
Y(int x){y=x;};
int get(){return y;}
private:
int y;
};
class X{
private:
int x;
public:
X(int m){x=m;};
X(X &y){x=y.get();};
~X(){};
int get(){return x;};
void set(int y){x=y;};
std:: ostream & operator <<(Y &y,std::ostream &os){os<<y.y;return os;};//想通过X类来访问显示Y类的数据
};
int main()
{
X x(1);
Y y=Y(1);
x<<(y,std::cout); //这里没有用友元函数来重载<<操作符,所以相当于调用 x.operator<<(y,std::cout)
return 0;
}