I have a class B with two overloaded functions int Set(B *); and int Set(const A&);. The class A expects a constructor argument unsigned char. When Set is called with a const unsigned char with value as 0, it is resolved to Set(B*) whereas when the value passed is non-zero, it resolves to Set(const A&) (as per my expectation).
The overload resolution works expectedly with non-const unsigned char but fails with const unsigned char with value set as 0. Why?
Following code illustrates the discrepancy when Set is called with const and non-const unsigned char
#include <iostream>
using namespace std;
class A{
char m_byteValue;
public:
A(unsigned char c) {
m_byteValue = c;
}
};
class B{
int m_a;
public:
B(){
m_a = 2;
}
int Set(B *);
int Set(const A&);
};
int B::Set(const A& v) {
cout << "I am in the const ref function\n";
return 0;
}
int B::Set(B* p) {
cout << "I am in the pointer function\n";
return 0;
}
int main(){
const unsigned char a = 0;
const unsigned char b = 1;
unsigned char c = 0;
unsigned char d = 1;
B var;
var.Set(a);
var.Set(b);
var.Set(c);
var.Set(d);
return 0;
}
Output (as compiled by gcc 4.9.2 c++98):
Demo - on ideone c++ 5.1
I am in the pointer function // Why?
I am in the const ref function
I am in the const ref function
I am in the const ref function