I'm quite new to C++ and I was wondering if there is any way around duplicating constructors, with variations in the passed arguments. I need to identify the correct given value (either float or int), check if the float can nicely be casted to an integer (e.g. 1.0) and call another constructor that accepts two integers if my values pass the test.
If anyone as any tips on improving this solution in general that would be great.
Fraction(int n, int d) : numerator(n), denominator(d) {
simplify(n, d);
}
Fraction(float n, float d) {
if (!isInteger(n) && !isInteger(d)) {
throw invalid_argument("Fractions only accept real numbers.");
} else {
Fraction(int(n), int(d));
}
}
Fraction(int n, float d) {
if (!isInteger(n) && !isInteger(d)) {
throw invalid_argument("Fractions only accept real numbers.");
} else {
Fraction(int(n), int(d));
}
}
Fraction(float n, int d) {
if (!isInteger(n) && !isInteger(d)) {
throw invalid_argument("Fractions only accept real numbers.");
} else {
Fraction(int(n), int(d));
}
}
float?floatto be passed which does hold an exact integer value?