On Sep 1, 11:52 pm, tom <pxk...@gmail.comwrote:
Sorry, my mistake (in the double->LongDouble conversion, is should be
a constructor), my question is actually for the code below:
#include <iostream>
class LongDouble
{
public:
// double -LongDouble conversion
LongDouble(double dval)
{
}
};
void calc(int a)
{
std::cout<<"void calc(int a) called"<<std::endl;
}
void calc(LongDouble a)
{
std::cout<<"void calc(LongDouble a)"<<std::endl;
}
int main(int argc, char *argv[])
{
double dval = 0.0;
calc(dval);
return 0;
}
In this case, converting the double argument to an int parameter is
considered a "better" match than converting the double argument to a
LongDouble parameter. According to the C++ Standard, standard
conversions (like double-to-int) have a higher rank than user-defined
conversions (such as double-to-LongDouble).
This point can be tested by adding another calc() overload:
void calc( long double ld) // Error: ambiguous
{
}
With this addition, there are now two possible standard conversions
(int-to-double and double-to-long double), so there is no clear
favorite anymore and the call to calc() is now ambiguous.
The best approach is probably not to mix user-defined and built-in
types in this manner. I would either have the program use the "long
double" built-in type instead of a LongDouble user-defined type - or -
I would keep the LongDouble type and define an Integer type to go
along with it.
Greg