I was a little surprised to learn this function in C ++, and I did not expect this to happen.
Here is the code:
struct XY {
int x,y;
XY(int v) : x(v), y(v) {}
};
bool test1(const XY &pos){
return pos.x < pos.y;
}
bool test1(int x, int y){
return x < y;
}
void functest(){
int val = 5;
test1(val);
}
Therefore, I can call a function with an integer parameter, regardless of whether or not such an overload exists, it will use a function of type XY, because it has a constructor of the same type! I do not want this to happen, what can I do to prevent this?
source
share