I have a piece of code with the following rough signature:
void evaluate(object * this)
{
static const int briefList[] = { CONSTANT_A, CONSTANT_Z };
static const int fullList[] = { CONSTANT_A, CONSTANT_B, ..., CONSTANT_Z};
const int const * pArray;
const int nElements;
int i;
if ( this->needDeepsEvaluation )
{
pArray = fullList;
nElements = sizeof(fullList) / sizeof(fullList[0]);
}
else
{
pArray = briefList;
nElements = sizeof(briefList) / sizeof(briefList[0]);
}
for ( i = nElements; i; i-- )
{
/* A thousand lines of optimized code */
}
this->needsDeepEvaluation = 0;
}
Most compilers will happily swallow the assignment of pArray, but chokes on the assignments of nElements. This inconsistency confuses me, and I would like to be enlightened.
I have no problem accepting that you can’t assign a const integer, but then why does it works as I expect for the const-pointer-to-const?
The fast and cheap fix is to drop the const qualifier, but that might introduce subtle bugs since much of the code inside the loop is macrofied (I’ve been bitten by that once). How would you restructure the above to allow a constant element counter?
In your declaration of
pArrayBoth ‘const’ keywords actually apply to
int. To get one to apply to the pointer, you’d have to declare it asint const * const pArray, in which the pointer itself becomes immutable. Your compiler should then throw an error on both assignments.