#include <iostream>
using std::cout;
class CheckConst
{
public:
void func(int* p)
{
cout << "func called";
}
void func(const int* p)
{
cout << "const func called";
}
};
int main()
{
int* p = nullptr;
CheckConst obj;
obj.func(p);
}
Above function overloading works fine. But why is the complier not complaining, both the function overloads are valid for function call for obj.func(p). If I remove either one of the function from the class, the code still works.
How is the complier distinguishing which one to call?
Because the first function
func(int* p)is an exact match via identity conversion while the second functionfunc(const int* p)requires a qualification conversion(aka conversion to const).Thus, second function is worst match than the first, for the argument
p.Basically, the first function doesn't require any conversion(aka identity conversion) for its argument
p.From over.ics.rank: