I have a static const array class member (const pointers to SDL_Surfaces, but that's irrelevant), and have to loop through it in order to populate it. Aside from a const_cast when I'm done looping, which I hear is bad practice, how would I go about doing this?
EDIT: The reason I don't just do...
static SDL_Surface *const myArray[3];
...
class::myArray[3] = {...};
is that I need to read from a different array and run a function on the different array's respective value in order to get the value for this array. Once I've looped all the way through, I'm never changing this array again, so the way I see it, it should be const.
EDIT 2: I think I might have made a conceptual mistake here. Is it possible to const_cast in some way to make something const, instead of to remove it's constness, which is what I was trying to do? If not, then I was being a little silly asking this :D
One method to provide "logical constness" is to make the data inaccessible, except by non-mutating means.
For example:
class foo
{
public:
const bar& get_bar() { return theBar; }
private:
static bar theBar;
};
Even though theBar
isn't constant, since foo
is the only thing that can modify it, as long as it does so correctly you essentially (logically) have a constant bar
.