After compilation, the program works fine and works as expected. Its output:
one
2
#include <stdio.h> class Foo { public: void Bar(const char* b, ...) { printf("1\n"); }; void Bar(int a, const char* b, ...) { printf("2\n"); }; }; int main() { Foo foo1; foo1.Bar("Test", "xx", 1, 2); foo1.Bar(1, "xx", "xx", 2, 2); }
Now, if I changed the int parameter of the second Bar function to bool and foo1.Bar(1, "xx", "xx", 2, 2); on foo1.Bar(true, "xx", "xx", 2, 2); , the following line will not compile, and I get an error: 'Foo::Bar': 2 overloads have similar conversions :
foo1.Bar("Test", "xx", 1, 2);
The whole program that does not compile:
#include <stdio.h> class Foo { public: void Bar(const char* b, ...) { printf("1\n"); }; void Bar(bool a, const char* b, ...) { printf("2\n"); }; }; int main() { Foo foo1; foo1.Bar("Test", "xx", 1, 2); // error: 'Foo::Bar': 2 overloads have similar conversions foo1.Bar(true, "xx", "xx", 2, 2); }
I do not understand why in the second case there is ambiguity.
EDIT
But if pointers are implicitly converted to bool , then why compile?
#include <stdio.h> class Foo { public: void Bar(const char* b) { printf("1\n"); }; void Bar(bool a) { printf("2\n"); }; }; int main() { Foo foo1; foo1.Bar("Test"); foo1.Bar(true); }