Why do you want to make the array const? If you are trying to protect it from the outside world, would you be able to make the array private and provide accessor functions so you have strict control over it?
The use of const_cast is nearly always a sign that something is wrong with the way a program is coded. (Usually meaning that initial authors didn't think about const issues, and later authors band-aided the resulting problems rather than taking the extra time to fix them.)
Rare examples to the contrary are the use of functions which ordinarily change the pointed-to-value, but which can be guaranteed not to in some specific instance where a side-affect is desired. For example:
Code:
class SomeMultiThreadedClass
{
...
public:
int GetCurrentValueOfSomeVariable(void) const
{
// Add zero to get the current value
return InterlockedExchangeAdd(const_cast<volatile LONG *>(&m_i), 0);
}
private:
volatile LONG m_i;
};
Another use is to get rid of a volatile qualifier, which is also a rare need.
Bookmarks