1.cout是在iostream中定义的,ostream类的对象
<<能用在cout上是因为在iostream中对<<进行了重载。所以我们只能把<<重载为全局函数。
那怎么重载才能使得cout<<cstudent和cout<<this都能成立呢?
class cstudent
{
public:int nage;
};
int main()
{
cstudent s;
s.nage=5;
cout<<s<<"hello";
return 0;
}
ostream & operator<<(ostream & o,const cstudent &s)
{
o<<s.nage; //o是cout的引用
return 0;
}