让我们这个代码来实现操作< <两类:超负荷运营商<<类模板
#include <iostream>
using std::cout;
using std::endl;
class A
{
int a1_;
public:
A(int a1) : a1_(a1){}
std::ostream& print(std::ostream& os) const
{
return os << "a1_ : " << a1_ << endl;
}
};
class B
{
int b1_;
double b2_;
public:
B(int b1,double b2) : b1_(b1),b2_(b2){}
std::ostream& print(std::ostream& os) const
{
os << "b1_ : " << b1_ << endl;
os << "b2_ : " << b2_ << endl;
return os;
}
};
std::ostream& operator<<(std::ostream& os, const A& in)
{
return in.print(os);
}
std::ostream& operator<<(std::ostream& os, const B& in)
{
return in.print(os);
}
int main(int argc,char* argv[])
{
A myA(10);
B myB(20,30.14);
cout << myA << myB << endl;
return 0;
}
因为我懒,我想运营商提供的模板版本< <代替如上所述的两个版本。我可以轻松取代:
template< class T>
std::ostream& operator<<(std::ostream& os, const T& in)
{
return in.print(os);
}
到目前为止这么好。如果我有几个班,我可以一次性实施运营商< <。当我的一个类是类模板时,麻烦就开始了。让我们以前面的例子,但与B级模板:
#include <iostream>
using std::cout;
using std::endl;
class A
{
int a1_;
public:
A(int a1) : a1_(a1){}
std::ostream& print(std::ostream& os) const
{
return os << "a1_ : " << a1_ << endl;
}
};
template <class T>
class B
{
int b1_;
T b2_;
public:
B(int b1,T b2) : b1_(b1),b2_(b2){}
std::ostream& print(std::ostream& os) const
{
os << "b1_ : " << b1_ << endl;
os << "b2_ : " << b2_ << endl;
return os;
}
};
std::ostream& operator<<(std::ostream& os, const A& in)
{
return in.print(os);
}
template <class T>
std::ostream& operator<<(std::ostream& os, const B<T>& in)
{
return in.print(os);
}
int main(int argc,char* argv[])
{
A myA(10);
B<A> myB(20,myA);
cout << myA << myB << endl;
return 0;
}
这个版本的作品,我有预期的结果,但是我已经提供了两个操作< <功能(每个班),让我们假设我有200个类已经实现了一个公共的ostream打印(ostream & os)const。其中一些是模板类(也有多个参数)。
在这种情况下,如何编写运营商的模板版本< <?
感谢您的帮助。
你的第一个代码也适用于类模板。但是,要注意它可以用于所有*类,它们不会定义自己的`operator <<`,这可能是不可取的。特别是,这样的函数已经在标准库的某个地方定义了,并且你必然违反了定义规则。 – 2011-02-07 16:22:46
@Konrad:你什么意思?那个人应该永远不会得到“没有运算符<< XXX”的错误? – UncleBens 2011-02-07 16:31:27