I have some code that looks like this:
void addPtrs(vector<Thing*> & ThingPtrs, const vector<Thing> & actualThings) {
for (const Thing & t : actualThings)
if (/*some condition*/)
ThingPtrs.push_back(&t);
}
Basically, I want to modify ThingPtrs
by giving it pointers to Thing
s that have some condition. However, because I do not want to modify the actual Thing
, I labeled actualThings
as const
, and now I can't assign a const
pointer to a nonconst
pointer.
Obviously one solution is to simply not make actualThings
const
, but I don't actually intend to modify it. Another solution is using a const_cast
, but I would like to avoid that if possible too.
What are my options in a situation like this?