Overloading operator << - C++

后端 未结 7 1656
Happy的楠姐
Happy的楠姐 2021-02-10 07:25

Background

I have a container class which uses vector internally. I have provided a method AddChar(std::string) to this wrap

7条回答
  •  借酒劲吻你
    2021-02-10 08:17

    The operator overloading in this case is not good practice as it makes the code less readable. The standard std::vector doesn't have it either for pushing elements, for good reasons.

    If you're worried about the caller code being too long, you could consider this instead of the overloaded operator:

    container.AddChar("First").AddChar("Second");
    

    This will be possible if you have AddChar() return *this.

    It's funny that you have this toString() function. In that case, an operator<< to output to a stream would be the standard thing to use instead! So if you want to use operators make the toString() function an operator<<.

提交回复
热议问题