Operator overloading and different types

前端 未结 5 1540
轮回少年
轮回少年 2021-01-04 09:33

I have a class Score which is going to be heavily used in comparisons against integers. I was planning on overloading the == operator to enable these comparisons as per th

5条回答
  •  猫巷女王i
    2021-01-04 09:56

    "Is this a sensible use of operator overloading?"

    If it makes your code easier to understand and doesn't cause problems, I would say, yes, absolutely! There may also be other ways, but if this works for you I don't see an issue.

    "Should I be providing overloads for the LH and RH sides of the operators to allow the usage to be symmetrical?"

    I would argue that unless you have a specific reason to do so, in other words if you are using or need them, then you aren't gonna need it. (YAGNI) The only real exception I see is if you are writing a framework where you have a pretty good idea that someone else is gonna need it.

提交回复
热议问题