Implicit type conversion rules in C++ operators

后端 未结 9 2094
情书的邮戳
情书的邮戳 2020-11-22 00:21

I want to be better about knowing when I should cast. What are the implicit type conversion rules in C++ when adding, multiplying, etc. For example,

int + fl         


        
9条回答
  •  情歌与酒
    2020-11-22 00:37

    The type of the expression, when not both parts are of the same type, will be converted to the biggest of both. The problem here is to understand which one is bigger than the other (it does not have anything to do with size in bytes).

    In expressions in which a real number and an integer number are involved, the integer will be promoted to real number. For example, in int + float, the type of the expression is float.

    The other difference are related to the capability of the type. For example, an expression involving an int and a long int will result of type long int.

提交回复
热议问题