Unable to access non-const member functions of objects in C++ std::set

后端 未结 3 1876
后悔当初
后悔当初 2020-12-07 04:32

Message is a class I made. I have a set of them in the main function that I pass to messageTimeOut (and some other functions). In messageTimeOut using an itorator I am loopi

3条回答
  •  生来不讨喜
    2020-12-07 05:02

    Objects in a set are const meaning that they are not mutable. Here are some options

    1) Create copies of the messages in a new set
    2) Remove the messages, mutate them, and put them back.
    3) Remove the "timeout" field from the message.
    

    Of these I prefer number 3. The fact that you are trying to mutate the messages is a "bad code smell". If you were to avoid all changing of data and instead create a new structure (for example a std::map) then you could reduce the amount of thread synchronization.

提交回复
热议问题