This is a case where you can use const_cast
to remove the const
-ness without violating the guarantee of your function.
// argument is const to prevent changing the item by this function
void doStuff(const Item *item)
{
// find index of the item inside the container
// indexOf() is declared as:
// template <typename T> int QList<T>::indexOf(const T &t, int from = 0) const
const int itemIndex = items->indexOf(const_cast<Item*>(item));
}
That's because indexOf
is merely finding the pointer in the container, not dereferencing the pointer and mutating what's on the other side.