I have a class Bwith two overloaded functions int Set(B *);and int Set(const A&);. The class Aexpects a constructor argument unsigned char. When Setcalled with const unsigned chara value like 0, it is allowed Set(B*), whereas when the passed value is non-zero, it is allowed Set(const A&)(as I expected).
Overload resolution works with non-const unsigned char, but with an error const unsigned charwith a value set to 0. Why?
The following code illustrates a mismatch when Setcalled with a constant and not a constantunsigned 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;
}
Conclusion (compiled using gcc 4.9.2C ++ 98):
Sample - ideone on C ++ 5.1
I am in the pointer function
I am in the const ref function
I am in the const ref function
I am in the const ref function