代码之家  ›  专栏  ›  技术社区  ›  shreyasva

C++中的流过载

  •  0
  • shreyasva  · 技术社区  · 14 年前

    为什么 void operator<<(ostream out, Test &t); 返回一个错误,而 void operator<<(ostream &out, Test &t); 不是吗?

    1 回复  |  直到 14 年前
        1
  •  10
  •   sbi    14 年前

    因为无法复制流,所以必须为每个引用传递流。

    注意的规范形式 operator<< 是这样的:

    std::ostream& operator<<(std::ostream& out, const Test &t)
    {
       // write t into out
       return out;
    }
    

    返回流很重要,这样您就可以将输出串在一起:

    std::cout << Test() << '\n';