用模板重载友元操作符即可
直接代码
class test
{
int out=0;
int in;
template<typename T> friend T& operator >>(T& instream, test t);
template<typename T> friend T& operator <<(T& outstream, test t);
};
template<typename T>
T& operator >>(T& instream, test t)
{
instream >> t.in;
return instream;
}
template<typename T>
T& operator <<(T& outstream, test t)
{
outstream << t.out;
return outstream;
}