Why is following not allowed in C++
#include <iostream>
class Sample {
public:
void Method(char x);
void Method(char const x);
};
void Sample::Method(char x) {
char y = x;
}
void Sample::Method(char const x) {
char y = x;
}
int main() {
Sample s;
return 0;
}
It doesn’t really answer why, but it is determined by the standard, §1.3.10
This just means the cv qualifiers of the arguments are ignored in the overload resolution.
A similar (but not equivalent) example with references works:
because here the types are different, the first case being a reference to
char, the second a reference toconst char(as opposed to aconstreference tochar).