Conversion Priority in C ++

I have the following code:

Some functions:

A::A(int i_a) {cout<<"int Ctor\n";}          //conversion constructor

void h(double d) {cout<<"double param\n";}   //f1
void h(A a) {cout<<"A param\n";}             //f2

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";
 //Without the const it will never try to convert
 }
 void main()
 {
 B b;
 cout <<"-----------------\n";
 f(b);
 cout <<"-----------------\n";
 h(1);
 }
+4
source share
2 answers

, - . ( ) . , .

( , ) ( ). (. ).

, int → double ( ), int → A ( ), f1.

: " " , ( ) - ( , , ++) ++: . , B , .

B.2:

, , . , , ....

, , , . ( ):

. , , (, const / ).

. , , const int ** int const * const *.

. - , (, bool, char, , ), int, unsigned int, long unsigned float .

. (, int float), .

. .

. ( , POD, undefined).

- - , :)

+14

(N4687)

2

( 16.3.3.1)

- (2.1) (16.3.3.1.1) , ,

- (2.2) a (16.3.3.1.2) (16.3.3.1.3).

0

All Articles