当作为成员函数重载时,a << b 被解释为a.operator<<(b),因此它只需要一个显式参数(this 作为隐藏参数)。
由于这要求重载是用作左侧操作数的类的一部分,因此对于普通的ostreams 等没有用。这将要求您的重载是ostream 类的一部分,而不是您的类的一部分。由于您不允许修改ostream,因此您不能这样做。这样就只剩下全局重载作为替代方案了。
但是,有一种相当广泛使用的模式,您可以在全局范围内重载运算符,但要调用成员函数:
class whatever {
// make this public, or the global overload a friend.
std::ostream &write(std::ostream &dest) const {
// write self to dest
}
};
std::ostream &operator<<(std::ostream &os, whatever const &w) {
return w.write(os);
}
当/如果您想要多态行为时,这特别有用。您不能使重载的运算符本身具有多态性,但您可以使其调用的成员函数virtual,因此无论如何它都具有多态性。
编辑:为了(我希望)澄清情况,您可以通过几种不同的方式来做到这一点。第一个可能也是最明显的是将我们的write 成员公开,并让全球运营商调用它。既然是公开的,我们不需要做任何特别的事情来让操作者使用它:
class myClass {
public:
std::ostream &write(std::ostream &os) const {
// write stuff to stream
return os;
}
};
std::ostream &operator<<(std::ostream &os, myClas const &m) {
// since `write` is public, we can call it without any problem.
return m.write(os);
}
第二种选择是将write设为私有,并声明operator<<为朋友以授予其访问权限:
class myClass {
// Note this is private:
std::ostream &write(std::ostream &os) const {
// write stuff to stream
return os;
}
// since `write` is private, we declare `operator<<` a friend to give it access:
friend std::ostream &operator<<(std::ostream &, myClass const &);
};
std::ostream &operator<<(std::ostream &os, myClas const &m) {
return m.write(os);
}
还有第三种可能性,几乎和第二种一样:
class myClass {
// Note this is private:
std::ostream &write(std::ostream &os) const {
// write stuff to stream
return os;
}
// since `write` is private, we declare `operator<<` a friend to give it access.
// We also implement it right here inside the class definition though:
friend std::ostream &operator<<(std::ostream &os, myClas const &m) {
return m.write(os);
}
};
这第三种情况在 C++ 中使用了一个相当奇怪(而且鲜为人知)的规则,称为“名称注入”。编译器知道 friend 函数不能成为类的一部分,因此不是定义成员函数,而是将函数的名称“注入”到周围的范围(在本例中为全局范围)。尽管operator<< 是在类定义中定义的,它根本不是一个成员函数——它是一个全局函数。