如何重载 << 运算符以使其从流中读取?

How can I overload the << operator to make it read from a stream?

本文关键字:lt 读取 重载 何重载 运算符      更新时间:2023-10-16

可能重复:
操作员过载

要重载>>的问题<运算符,让它们从流中读/写?

有人能解释一下如何做到这一点吗?

定义

std::ostream &operator<<(std::ostream &out, Foo const &x)
{
    // write a representation of x to out
    // you can use << on x's members
    return out;
}
std::istream &operator>>(std::istream &in, Foo &x)
{
    // read a representation of a Foo from in
    // and use it to modify x
    return in;
}

适当地。

您不是只定义operator >>operator <<吗?