I have a doubt on the result of the question below. When calling function f
from main, the call goes to f(B &)
although I have int operator overloaded and f(int)
is defined first.
If I comment out the f(B &)
function, then the call goes to f(int)
and prints 5.
#include <iostream>
using namespace std;
class A{
int y;
public:
A(int y=2):y(y){}
int getValue(){
cout<<y<<endl;
}
};
class B{
int x;
public:
A a;
B(int x=5):x(x){}
operator int(){
return x;
}
};
void f(int x){
cout<<x<<endl;
}
void f(B &b){
b.a.getValue();
}
int main() {
B b;
f(b);
}
I was expecting it to go to f(int)
function and print 5
but it instead prints 2
. Why it does not go to f(int)
instead of f(B &)
. Why is this behavior happening, can anyone please explain?