[Qt-interest] QFlags and conditional assignment
Thiago Macieira
thiago at kde.org
Thu Feb 25 14:16:18 CET 2010
Em Quinta-feira 25 Fevereiro 2010, às 12:30:57, Ulf-Alexander v. Ceumern-
Lindenstjerna escreveu:
> This is what I do:
>
> m_scoringOptions = 0; // works!
Note how the literal 0 is special. QFlags defines no operator=, so the only one
that exists is the implied operator=(const QFlags &). That means the compiler
actually ran for you:
m_scoringOptions.operator=(QFlags(0));
Which in turn called the constructor:
inline QFlags(Zero = 0) : i(0) {}
And QFlags::Zero is defined as:
typedef void **Zero;
So, that 0 was actually treated like a pointer.
> bool ok = true;
> m_scoringOptions = ok ? SCORING_FUNDUS : SCORING_DISABLED; //works!
Here, what happened was that the operator ?: returned ScoringOption, so the
compiler used the constructor:
inline QFlags(Enum f) : i(f) {}
Where Enum is your enum (the template argument).
> m_scoringOptions = ok ? SCORING_FUNDUS : 0; // error:
>
> 1>.\src\myclass.cpp(150) : error C2679: binary '=' : no operator found
> which takes a right-hand operand of type 'int' (or there is no acceptable
> conversion)
Here what happened was that ?: had to return int. It happens because the
second argument to ?: was ScoringOption and the third was zero. There's no
defined conversion from zero to an enum, so the compiler had to demote the enum
to int instead.
The end result was that you tried to call the QFlags constructor with an
argument of int and there's no such constructor (or one for which the compiler
could cast the int to).
If we added such a constructor, you'd be able to write the following without
compiler errors:
m_scoringOptions = 1234;
So the solution for you is to properly cast to the enum. Two possibilities:
m_scoringOptions = ScoringOption(ok ? SCORING_FUNDUS : 0);
or
m_scoringOptions = ok ? SCORING_FUNDUS : ScoringOption(0);
Or, like you had written before, using SCORING_DISABLED (which has a value of
0).
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Senior Product Manager - Nokia, Qt Development Frameworks
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100225/d3f04f8a/attachment.bin
More information about the Qt-interest-old
mailing list