In C++, if char
to int
is implicit conversion and int
to char
is also implicit conversion. Normally, a char
is single or double byte. On 32-bit machine an int
is usually 4-byte. Thus, int to char
conversion has the possibility of data loss (as moving from bigger area to smaller storage area), where as char to int
is more logical candidate for implicit conversion.
Now, what about this?
Say, we attempt to overload functions with arguments as (int, char)
and another as (char, int)
, if I call the function with (char, char)
, would this be ambiguous or call to (char, int)
is more logical?
For example,
void f(char a, int b)
{
cout << "f(char, int) called" << endl;
}
void f(int a, char b)
{
cout << "f(int, char) called " << endl;
}
int main()
{
char a = 'a', b = 'b';
f(a, b); // which overloaded function should be called?
return 0;
}