I am new to C++ and I am trying to modify some existing code. I basically have to modify a const reference variable in C++. Is there a way to do so?
I want to remove a subtring from a constant string reference. This obviously wouldn't work, since id is a constant reference. What would be the correct way to modify id? Thanks.
const std::string& id = some_reader->Key();
int start_index = id.find("something");
id.erase(start_index, 3);
const_cast<std::string&>(id).erase(start_index,3). I'll let other people tell you why this is a bad idea.constmodifier.