Why in conditional operator (?:), second and third operands must have the same type?

后端 未结 5 1383
挽巷
挽巷 2021-01-31 19:35

Why in conditional operator(?:), second and third operands must have the same type?

My code like this:

#include 

        
5条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 19:38

    It's a strong type language and it need to treat the whole ?: as a variable or statement, like:

    int i = 3;
    int j = 5;
    int k = (2 > 1 ? i : j) + 3;
    

    in such a case, what would you expect it should do for you if i is a string?

提交回复
热议问题