Dec 9, 2010 at 8:19pm UTC
I have:
1 2 3
bool dew = true ;
if (dew--)
dewsomething();
but I get
invalid use of boolean expression as operand to operator--
I could change dew to int I guess. But I was thinking about how would I go about doing something like this:
1 2 3
bool dew = true ;
if (((int )dew)--)
dewsomething();
because I get
lvalue required as decrement operand
.
I want to be able to execute a specific function only on the very first iteration of a loop. Yes the bool dew is declared outside of the loop.
And I need to be able to use the post decrement operator because
dew-=1
won't cut it.
And I don't want to put the function before the loop, because I may ask the user if they want to re-execute the function later on.
Edit:
And this one I really don't get. I still get
lvalue required as left operand of assignment
with
if (dew && dew = false )
Last edited on Dec 9, 2010 at 8:29pm UTC
Dec 9, 2010 at 8:27pm UTC
1 2 3
//bool *p = &dew;
//if( (*(char *)p)-- )
if ( (*(char *) &dew)-- )
Last edited on Dec 9, 2010 at 8:30pm UTC
Dec 9, 2010 at 8:29pm UTC
thanks ne555
that is really really amazing. I can't believe it worked. You guys are great!!
Last edited on Dec 9, 2010 at 8:37pm UTC
Dec 9, 2010 at 8:34pm UTC
doesn't the assignment operation itself return true when successful?
it lets me compile: if (dew -= 0)
but not if (dew && dew-=1)
Last edited on Dec 9, 2010 at 8:36pm UTC
Dec 9, 2010 at 8:43pm UTC
operator precedence. &&
has higher precedence than assignment
dew && dew =-1;//equivalent to (dew && dew) =-1;
Edit: The assignment doesn't return true
when successful (I wonder when it could fail). It returns the result of the operation (i.e. *this
)
Last edited on Dec 9, 2010 at 8:51pm UTC
Dec 9, 2010 at 9:52pm UTC
I see that if ( dew && ( (*(char *) &dew)--) )
is even better than if ( (*(char *) &dew)-- )
because on repetitive calls in a loop in an (unsigned?) bool anything past negative gets converted to 255, which will then be interprted as true.