Operator overload of << needs const; produces headache

前端 未结 4 1046
天命终不由人
天命终不由人 2021-01-24 21:54

I am trying to overload operator <<, but it always need to be a const function. However, I want to change values inside this overloaded function

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-24 22:14

    You're likely doing something wrong then. Post the relevant bits of your code, along with a better summary of what your trying to do, and you'll get more help.

    That being said, if you need to modify some data in a const object, there are a couple things you can do:

    1. Declare the member you want to modify mutable.
    2. Use const_cast<...>(...) to remove the const modifier from the passed in object.

    But most likely you're trying to do something the wrong way.

提交回复
热议问题