Why is the following not allowed in C ++?
The reason is that the compiler gives you a compilation error:
Because they are ambiguous!
Why are these methods ambiguous?
Short answer: Because the C ++ standard says so.
What rationale for these overloaded methods is ambiguous?
The compiler does not know whether the caller wants to parse the value of the argument passed as a const
or not, the compiler can not determine that from this information. p>
Note the emphasis on pass by value here, the argument is passed by value and therefore ambiguity. If the argument was passed by reference , then the compiler knows exactly how the caller should handle the argument, because then the actual object itself is passed, and therefore the compiler can choose the right overload.
The following example gives a clearer picture of this explanation.
Online example :
class Sample { public: void Method(char &x){} void Method(char const x){} void Method(char const &x){} }; int main() { Sample s; return 0; }
source share