fun(char*) and fun(char)
-
Hi: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? I also define fun(int) and fun(int*),to my surprise,I call fun(0) in the same way. The compiler compile successfully and select fun(int),Why? your friend:bobi
-
Hi: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? I also define fun(int) and fun(int*),to my surprise,I call fun(0) in the same way. The compiler compile successfully and select fun(int),Why? your friend:bobi
I'm not a compiler expert, but suppose that the compiler tries to convert (cast) the input argument to something. Since 0 is by default an integer it tries to cast to char but it also can be casted to NULL pointer. So the compiler doesn't know which is the best solution. In the second case the 0 is an integer and the compiler found the method, doesn't need conversion (casting). I suppose if you call the function as fun(0L) it will have the same result as in the previous case... To resolve the problem call the function as
fun((char)0)
. Hope this help, Abyss -
I'm not a compiler expert, but suppose that the compiler tries to convert (cast) the input argument to something. Since 0 is by default an integer it tries to cast to char but it also can be casted to NULL pointer. So the compiler doesn't know which is the best solution. In the second case the 0 is an integer and the compiler found the method, doesn't need conversion (casting). I suppose if you call the function as fun(0L) it will have the same result as in the previous case... To resolve the problem call the function as
fun((char)0)
. Hope this help, Abyssi also though of an implicit cast (that is what the compiler does actually), but there should not be any difference between
0
and1
in calling the function. if 0 is casted to char, so 1 should be too. if 0 is casted to char* (NULL pointer), then 1 should be casted to address 1...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
I'm not a compiler expert, but suppose that the compiler tries to convert (cast) the input argument to something. Since 0 is by default an integer it tries to cast to char but it also can be casted to NULL pointer. So the compiler doesn't know which is the best solution. In the second case the 0 is an integer and the compiler found the method, doesn't need conversion (casting). I suppose if you call the function as fun(0L) it will have the same result as in the previous case... To resolve the problem call the function as
fun((char)0)
. Hope this help, Abyss -
Hi: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? I also define fun(int) and fun(int*),to my surprise,I call fun(0) in the same way. The compiler compile successfully and select fun(int),Why? your friend:bobi
bobi_zcl wrote: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? Was this not answered sufficiently here and here? 0 is an integral constant expression. For the
char
/char*
case, neither is a better match for the 0 than the other, hence the ambiguity error. For theint
/int*
case,fun(int)
is an exact match for 0, thus no error.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
bobi_zcl wrote: I overload funcion fun(char) and fun(char*),when I call fun(0),the compiler said:" ambiguous called ....",but when I call fun(1),the compiler will select fun(char).why? Was this not answered sufficiently here and here? 0 is an integral constant expression. For the
char
/char*
case, neither is a better match for the 0 than the other, hence the ambiguity error. For theint
/int*
case,fun(int)
is an exact match for 0, thus no error.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow