64,676
社区成员
发帖
与我相关
我的任务
分享
template <typename T>
class A {
private :
T a ;
public :
inline A () {}
inline A (const T &a) :a (a) {}
#ifdef _IOSTREAM_
friend inline istream &operator>> (istream &is ,A &a) {
return is >> a.a ;
}
friend inline ostream &operator>> (ostream &os ,const A &a) {
return os << a.a ;
}
#endif
} ;
比如说Xml类函装Xml操作,而你想让Xml类能操作类M
无非是通过接口或者友元重载 ,这样让Xml操作M的函数是由M类给出
这样怎么会有模板参数的问题呢,如上例
换过来想,如果M没有实现Xml接口,Xml也没有对M的操作,让第三方搭桥,有这样的设计吗?
class xml_input_stream;
class xml_output_stream;
xml_input_stream& operator >>( xml_input_stream, int& v);
xml_output_stream& operator <<( xml_output_stream, int v);
.....
int x = 5;
xml_output_stream xos = ....;
xos << 5;
...
int y;
xml_input_stream xis = ....;
xis >> 5;
运算符重载吧, 就如同C++ 标准输入输出流。