Jason said:
I just found one way to fix my ambiguous constructors. You need to add
a dummy int parameter to one of the constructors. When you want to
call that particular constructor, you specify a zero argument.
I'd rather use an enum to make the resulting code more readable:
class Length
{
public:
enum Unit { Centimeter, Inch };
Length(double l, Unit u = Centimeter);
//...
};
However, you will need to select (with if or switch/case) the code
depending on the value.
If you want two separate constructors, you can do it too with a small
modification:
class Length
{
public:
enum Centimeter_t { Centimeter };
enum Inch_t { Inch };
Length(double l, Centimeter_t = Centimeter);
Length(double l, Inch_t);
//...
};
Now you can write:
Length l1(3); // 3 centimeters, constructor 1
Length l2(5, Inch); // 5 inches, constructor 2
Length l3(7, Centimeter); // 7 centimeters, constructor 1
If you leave out the default value, you can enforce the explicit
specification of the unit, and the definition of l1 becomes invalid.