[KUnitConversion] Question about overloading operators

Ander Pijoan ander.pijoan at deusto.es
Wed Nov 23 08:00:10 UTC 2016


So would it be to hamrfull to add at least this four operators? At the
moment just allowing same Unit values. You mentioned that using Q_ASSERT is
not a good idea, maybe there is another way of ensuring they have the same
unit.

Value operator +(const Value &other) {
Q_ASSERT( this->unit() == other.unit() );
return Value( this->number() + other.number() , this->unit() );
}

Value operator -(const Value &other) {
Q_ASSERT( this->unit() == other.unit() );
return Value( this->number() - other.number() , this->unit() );
}

Value operator *(const double v) {
return Value( this->number() * v , this->unit() );
}

Value operator /(const double v) {
return Value( this->number() / v , this->unit() );
}

I think, this four operators could be really usefull and are pretty simple.

2016-11-21 22:59 GMT+01:00 Albert Astals Cid <aacid at kde.org>:

> El dilluns, 21 de novembre de 2016, a les 18:00:18 CET, Christoph Feck va
> escriure:
> > On 21.11.2016 08:27, Ander Pijoan wrote:
> > > Value operator +(const Value &other)
> > > {
> > >
> > >       Q_ASSERT( unit() == other.unit() );
> > >       return Value(  number() + other.number() , unit()  );
> > >
> > > }
> >
> > So when adding "3 m" and "40 cm" you want to have an assert (that isn't
> > compiled into code unless you are in debug builds)?
> >
> > Also, for multiplication, you would need to add all relationships
> > between units, so that "40 mph" * "30 min" would result in "20 miles".
> >
> > This is especially problematic for temporary units, e.g. "40 kg" * "20
> > m/s" * "30 Hz" should have a "Newton" unit result, but just multiplying
> > mass and speed has no unit assigned in KUnitConversion.
>
> We have a InvalidUnit, you can always default to that for stuff "you don't
> know".
>
> Cheers,
>   Albert
>
>


-- 

Ander Pijoan

Research Assistant / DeustoTech EnergĂ­a

Universidad de Deusto / Deustuko Unibertsitatea

Avda. Universidades 24. 48007 Bilbao

Tel. 94 413 90 03/ 2052
ander.pijoan at deusto.es
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-frameworks-devel/attachments/20161123/df453da8/attachment.html>


More information about the Kde-frameworks-devel mailing list