Does not surprise me.. type converted items are generally r-value only.
(l = left, r = right, as to where they may be used in the = operator)
In the code as shown the compiler is wise to at least warn about the use
of ++ and should preferably not allow such code as it is quite likely
not what the programmer intended in more complex cases. The assignment
side effect of ++ is discarded there as it is done on a temporary
r-value int object who is not saved anywhere (not even in a local).
A more sane implementation of the snippet below is
aLogType = (log_type) ((int)aLogType) + 1;
or expanded similar to your proposal to avoid deep nesting of type
conversions:
int i = (int)aLogType;
aLogType = (aLogType)(i + 1);
Alternatively the operator can be implemented as a friend who is allowed
to operate directly on the internal representation of aLogType to
implement ++.
Regards
Henrik
mån 2002-11-25 klockan 09.46 skrev Guido Serassio:
> This is the not working code:
>
> log_type &operator++ (log_type &aLogType)
> {
> aLogType = (log_type)++((int)aLogType);
> return aLogType;
> }
>
> Compiling...
> client_db.cc
> C:\work\nt-3.0\src\client_db.cc(163) : error C2105: '++' needs l-value
> Error executing cl.exe.
>
> This works:
>
> log_type &operator++ (log_type &aLogType)
> {
> int i = (int)aLogType;
> aLogType = (log_type)(++i);
> return aLogType;
> }
>
> Regards
>
> Guido
Received on Mon Nov 25 2002 - 03:27:18 MST
This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:18:49 MST