[Development] Converting types in Qt
Olivier Goffart
olivier at woboq.com
Thu Jul 17 14:36:22 CEST 2014
On Thursday 17 July 2014 13:33:49 Daniel Teske wrote:
> A equals operator that is not symetric is broken. Such a class cannot be
> reliably used in std nor qt containers. Or do you know which way around,
> QList::contains uses the equals operation?
Note that none of the class which have a member operator== is symmetric.
Most of the class had an operator== as a member in Qt4 but that was a design
mistake. New classes should have a friend operator== instead.
Example:
qDebug() << (QUrl() == QString()); // true
qDebug() << (QString() == QUrl()); // compilation error
That's because QUrl has a
bool QUrl::operator==(const QUrl&);
Instead of
bool operator==(const QUrl &, const QUrl&);
The later is symmetric, but the former is not.
This is also valid for operator< and related.
(That was just to go a bit more out of topic :-))
--
Olivier
Woboq - Qt services and support - http://woboq.com - http://code.woboq.org
More information about the Development
mailing list