I have a some pieces of code in my project in which a structure z is being used. This structure declaration changes as per a condition flag being set in my system. I want the structure declaration to be dynamic.
if condition flag is 0 then,
struct x z;
if condition flag is 1 then,
struct y z;
but the operation to be performed on z would be the same. Currently this is handled by declaring both the structure :
struct x z_x;
struct y z_y;
then as per the condition flag use the appropriate structure in the code. This method doesn’t seem to be optimal. Any suggestions on handling this type of code ?
As the flag is run-time, you cannot avoid checking this flag all the time. At least, I can’t think of such way.
But you still can reduce the memory and the code. I’d do it like this:
To reduce the memory usage:
To reduce the code, instead of writing all the time things like:
I’d write:
where
of course,
operations_1andflagshould have some good and logical names.BUT this could lead do some problems, so just be careful, if you decide to use this approach.