Possible Duplicate:
Is there some ninja trick to make a variable constant after its declaration?
Sometimes in C or C++ we have a variable that might be const, but we have to take some lines of code to initialize it.
Is there a way to tell the compiler that, from some point in a function, some already constructed variable has to be considered as const, until its scope ends?
Something like:
int c = 0;
// the value of c is initialized here
switch(someVar) {
case foo: c = 3; break;
case bar: c = 4; break;
default : c = 42; // what else?
}
// now c is constant
ASSUME_CONST_FROM_NOW(c) // some #pragma maybe?
I know that I could initialize the variable in a dedicated function. This is not really what I am asking for.
Another example:
int c = 0; int d = 0;
{ /*some block of code that initializes both c and d jointly*/ }
ASSUME_CONST_FROM_NOW(c, d)
There is no function that can return two values at a time without creating structures or classes.
But such a trick could be useful in order to make old, crappy code more easily understandable with not much refactoring.