Postings by Mathias on September 15, 2010
Operator Overloading
Just wondered right now why Qt doesn't provide a greater-than operator for QSize.
Well, indeed: How would you define this operator? Maybe like this?
inline bool
operator >(const QSize &a, const QSize &b)
{
return a.width() * a.height() > b.width() * b.height();
}
Or is this the proper definition?
inline bool
operator >(const QSize &a, const QSize &b)
{
return (a.width() > b.width() || a.height() > b.height());
}
Mathematician might intuitively choose the first alternative, aka. covered area. I claim for UI problems usually the second interpretation is useful.
Funnily the Qt author(s) of QSize implicitly agree with my claim, as they provide:
inline bool QSize::isValid() const
{
return wd>=0 && ht>=0;
}
Which gives "(b - a).isValid()" computing the same result as my preferred interpretion of the greater-than operator.
Well, my currently preferred interpretion, within the scope of my current problem. Oh, and sans integer overflows and such "minor problems" - of course. Someone really cares about such "minor details"? :-)
So what tells this? API design is fun. Even more if you add operator overloading to the soup.
*Disclaimer: There is nothing Qt specific in this post. It only provides the example. *