This is a somewhat trivial temperature scale example.
Something that I've seen used occasionally are enum classes. Something like:
enum class temperature_scale {CELSIUS, FAHRENHEIT};
However, when you'd want to use that, you'd probably want a way to convert. Say you're using this with some class, temperature, and you store the temperature internally in celsius (to easily add or compare temperatures), but you want a way to let the user pass in a temperature in a different base.
temperature::temperature(double tmp, temperature_scale scale);
And in there, you'd probably have some kind of switch statement. Or, you might realize that having switch statements everywhere is a bad idea, and simply have some kind of to_celsius() and from_celsius() function which would keep the switch statements in one place and do the conversions for you.
Something that Java enums allow for is polymorphic behavior. They can implement interfaces, so you could have to_celsius() and from_celsius() as methods on the enums themselves, and rather than some kind of switch statement, you'd just defer to the enum to do the conversion directly.
I thought about how to approximate this in C++, and I came up with
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class temperature_scale {
public:
virtual double to_celsius(double n) const = 0;
virtual double from_celsius(double n) const = 0;
virtual ~temperature_scale() {}
};
namespace temperature_scales {
const class : public temperature_scale {
public:
virtual double to_celsius(double n) const {
return n - 273;
}
virtual double from_celsius(double n) const {
return n + 273;
}
} KELVIN;
}
|
I only listed KELVIN, but CELSIUS and FAHRENHEIT would be provided as well. I tried this, and it seems to do what I want, and it works similarly to a Java enum:
temperature_scale& scale = temperature_scales::KELVIN;
(though obviously they are not singletons like Java enums).
I was just wondering - is there any problems with doing something like this? I haven't really seen anything like this in C++ (not that I've seen that much C++ anyway), but it seems fairly obvious to me. So, it makes me think that there's some problem with this I'm overlooking.