基础模板:
ostream& ostream::operator<<(int n)
{
//...输出n的代码
return *this;
}
ostream& ostream::operator<<(const char* s)
{
//输出s的代码
return *this;
}
升级模板:
#include<iostream>
using namespace std;
class CStudent
{
public:
int nAge;
friend ostream& operator<<(ostream& a,const CStudent& b);
};
ostream& operator<<(ostream& a,const CStudent& b)
{
a<<b.nAge;
return a;
}
int main()
{
CStudent a;
a.nAge=5;
cout<<a<<"HEllo";
return 0;
}