I have the following code:
Some functions:
A::A(int i_a) {cout<<"int Ctor\n";}
void h(double d) {cout<<"double param\n";}
void h(A a) {cout<<"A param\n";}
In the main function:
h(1);
The function h (1) calls f1.
My question is why he calls it. 1 is int and therefore requires an implicit conversion to double. It can also easily convert an int to A using the conversion constructor defined above. Why am I not getting an error message? What are cast priority rules?
Nb I placed above the code, which, in my opinion, will be necessary to answer the question, but below I send all the code:
#include <iostream>
using namespace std;
class B;
class A {
public:
explicit A(const B&) {cout<<"Ctor through B\n";}
A() {cout<<"Default Ctor\n";}
A(int i_a) {cout<<"int Ctor\n";}
operator int() {cout<<"A => int\n"; return 2;}
};
class B {
public:
operator A() const {cout<<"B => A\n"; A a; return a;}
};
void h(double d) {cout<<"double param\n";}
void h(A a) {cout<<"A param\n";}
void f(const A& a)
{
cout<<"f function\n";
}
void main()
{
B b;
cout <<"-----------------\n";
f(b);
cout <<"-----------------\n";
h(1);
}
source
share