Hello, gods of c++! I beseech you, lay upon me your vast and infinite knowledge of purity of code.
It all started with this:
Code:
class Level {
public:
list<Rock *> rocks;
...
};
and when I have a const pointer to the Level, I shouldn't be able to modify anything it contained, including any rock in the rocks list. This should have failed:
Code:
for (list<Rock *>::const_iterator i = level->rocks.begin(); i != level->rocks.end(); i++)
(*i)->rockRadius *= 2;
Of course, it compiled successfully, because while the list may have been constant, it didn't hold pointers to const rocks, just regular rocks. Changing the list to be a list<const Rock *> wouldn't work, because then my Level internals couldn't modify it.
So I changed to this:
Code:
class Level {
list<Rock *> rocks;
public:
const list<const Rock *> *getRocks() const { return (const list<const Rock *> *)&rocks; }
...
};
And this works fine. Two problems though:
1. It requires a dirty cast.
2. It's misleading, it tells the user, "yeah, go ahead and add your const stuff to this list, it won't be modified.", but it's just a lie--it's a pointer to non-const stuff.
What's the best way to design something like this?
Thanks!