From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2007-03-28 14:40:28


Michael Fawcett wrote:
> On 3/27/07, Noah Roberts <roberts.noah_at_[hidden]> wrote:
>> Nope. Your dialog manager would keep track of the unit and set
>> appropriately upon initialization and/or user interaction. Your reader
>> would then be as simple as setting the value directly to the quantity.
>> Alternatively you might have something like this:
>>
>> quantity<abstract> on_dialog_ok()
>> {
>> quantity<abstract> dist;
>> dist.set_unit(combo.selected_item().data());
>> dist.set_value(entry.value().as_double());
>> }
>
> I see. Would the implementation allocate a derived class based on the
> unit type, or just dispatch during arithmetic based on unit type? I
> suppose it works out to about the same penalty either way, virtual
> function or if statement for every arithmetic. It would probably be a
> lot cleaner to go the derived class route though.

No virtual function would be required.

template < typename D >
struct unit
{
   double convert(double base_value) const { return base_value * cv_fact; }
  private:
   double cv_fact;
};

D is the dimension and simply makes sure you don't intermix dimensions
as it would be unassignable to any quantity except ones in the same
dimension.