64,649
社区成员
发帖
与我相关
我的任务
分享
#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;
class A{
public:
A(int i,const string &s):i(i),s(s){}
// 定义为成员函数时,this指针绑定到左操作数,所以用起来很不习惯哦
ostream& operator<<(ostream &os)
{
os<<i<<s;
return os;
}
private:
int i;
string s;
};
int main(int argc,char* argv[])
{
A a(88,"abc");
a<<cout; // 不习惯...
system("pause");
}
#include <iostream>
#include <string>
using namespace std;
class A{
public:
A(int i,const string &s):i(i),s(s){}
// 定义为成员函数时,this指针绑定到左操作数,所以用起来很不习惯哦
ostream& operator<<(ostream &os)
{
os<<i<<s;
return os;
}
friend ostream& operator << (ostream &os, const A& a)
{
return os << a.i << a.s;
}
private:
int i;
string s;
};
int main(int argc,char* argv[])
{
A a(88,"abc");
a<<cout; // 不习惯...
cout<<endl<<a<<endl; // 这样就习惯了吧
system("pause");
}