mardi 5 mai 2020

C ++ function matching (priority of built-in conversion and class type conversion)

i have this code :

class A
      {
      public:
operator int() const
{
    return 100;
}
int operator+ ( int a)
{
    return 10 + a;
}
};

int main()
{
A a;
int b = a + 11.5;
}

this code the compiler told me that calling "+" is ambiguous but only have two ways calling "+" i think: first : use class type conversion, convert object a to int type second : Convert 11.5 to type int, then use the class operator "+"

I think the priority of the second case is higher than the first one, but why is there ambiguity?

Aucun commentaire:

Enregistrer un commentaire