Basic code obfuscation

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
1
2
3
//bool *p = &dew;
//if( (*(char *)p)-- )
if( (*(char *) &dew)-- )
Last edited on
thanks ne555
that is really really amazing. I can't believe it worked. You guys are great!!
Last edited on
if (dew && dew = false)
What do you want to do? operator= is assignment
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
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
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.
Topic archived. No new replies allowed.