c++ overloading stream operator, parameters by reference and anonymous instances

前端 未结 1 1552
予麋鹿
予麋鹿 2021-01-20 10:19

If I have a POD with an overloaded stream operator:

struct Value{
...
    friend ostream& operator<< (ostream &out, Value &val);
...
};


        
相关标签:
1条回答
  • 2021-01-20 11:22

    Since the operator should not modify the right operand, it should take it by const reference:

    friend ostream& operator<< (ostream &out, const Value &val);
    

    const references can bind to temporaries, so it will work (and that's how the standard library does it as well).

    0 讨论(0)
提交回复
热议问题