The following program compiles perfectly, even though the method "m", which is supposed to receive an instance of C, is called with an integer or a float.
class C {
public:
const int value;
C(int value) : value(value) {}
};
void m(C parameter) {
}
int main(void) {
m(0);
m(0.123);
return 0;
}
If I call the method "m" with a string, I get this error message below. The error message indicates that the compiler is actually trying to build a class C from the string.
How is this possible? Is there a way to disable this behavior? How can I ensure that method "m" can only accept a parameter of class C?
Thank you !
J
test.cpp: In function ‘int main()’:
test.cpp:13:5: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
13 | m("");
| ^~
| |
| const char*
test.cpp:4:9: note: initializing argument 1 of ‘C::C(int)’
4 | C(int value) : value(value) {}
Error message, saying method "m" accepts only instances of class C, not integers.