In math, if z = x + y / 2
, then z
will always change whenever we replace the value of x
and y
. Can we do that in programming
You can get what you're asking for by using macros:
{
int x, y;
#define z (x + y)
/* use x, y, z */
#undef z
}
The #undef
is for a little sanity. For more sanity, don't use macros at all, and go with one of the other answers, and deal with the extra verbosity.
Although a class with a custom operator int
would work in a lot of cases ... hmm.