Possible Duplicate:
Is there some ninja trick to make a variable constant after its declaration?
Consider the following minimal example:
void MutateData(std::string&);
int main()
{
std::string data = "something that makes sense to humans.";
::MutateData(data); // Mutates 'data' -- e.g., only changes the order of the characters.
// At this point, 'data' should never be changed.
// Mixing the above with const-correct code seems ugly.
}
Currently, I'm doing:
namespace
{
std::string PrepareData(std::string data)
{
::MutateData(data);
return data;
}
}
int main()
{
const std::string data = ::PrepareData("something that makes sense to humans.");
}
What are some elegant solutions to simulating const
beyond the point of declaration?
EDIT: I forgot to clarify that I can't easily (not my code) change MutateData
.