Is it allowed to name the parameter in postfix operator ++?

后端 未结 3 601
借酒劲吻你
借酒劲吻你 2021-01-01 11:39

I am not using this code in any production environment, it is just for my understanding. Is this code valid (i.e. can I define my postfix operator like this?):



        
相关标签:
3条回答
  • 2021-01-01 12:25

    Yes, it is valid the int as a parameter it only a policy enforcing parameter to distinguish between prefix and postfix operators. The passed parameter will be received as an argument, which is the behavior you see & it is prfectly defined behavior.

    0 讨论(0)
  • 2021-01-01 12:33

    a++ is equivalent to a.operator++(0);. Your code is valid

    13.5/7

    When the postfix increment is called as a result of using the ++ operator, the int argument will have value zero.

    0 讨论(0)
  • 2021-01-01 12:43

    This behavior is legal and well defined in 13.5.7:

    Calling operator++ explicitly, as in expressions like a.operator++(2), has no special properties: The argument to operator++ is 2.

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