So I can tell you a bit about one of the problems: I have a LengthUnit class that serves as base of all units like MeterUnit, FootUnit, InchUnit, etc. But let's face it: I don't want people redefining what a Meter is, right? So I added private constructors and then a single static const field in each class: FootUnit::Foot is an instance of FootUnit that represents a foot.
But prefixes (kilo, Mega, Giga, deci, centi, etc.) makes things more complex. To further explain:
A particular instance of a Length class (that contains a double value and a polymorphic LengthUnit* unit) may alter the unit adding a prefix because maybe they want kilometers. I don't want the developer using the library to be concerned about the storage of this new instance of MeterUnit (which will be copy-constructed from MeterUnit::Meter). The developer should be able to say something like:
1 2
|
Length myHeight(1.74, MeterUnit::Meter);
myHeight.ConvertTo(MeterUnit(MeterUnit::Meter, Prefix::milli)); //convert to millimeters
|
They have to create a new instance because MeterUnit::Meter is const and a prefix cannot be added.
Internally, all Length objects save a pointer of type LengthUnit because a reference cannot be changed, and it has to be a pointer or reference because it is polymorphic. Finally, the question: Where does the new unit is to be stored? Note that in the example above I am using a new instance of MeterUnit, but I could have used a const like FootUnit::Foot.
Or maybe I have been thinking this the wrong way?