It looks like QList
is designed to be used on non-pointers. They define a lot of the interface that is const as const T&
which would work great if your QList
was on Child
, not Child*
.
It will work on pointers just fine, but it can't declare the constness right for them. I don't recommend changing your QList to Child unless it's cheap to copy, you have all the right semantics for copy ctor, dtor, op=, op==, etc and you don't mind having copies in the list rather than the objects you pass in. You can see how with int
or strings, that it would work as expected (removeAll would be const correct).
If const correctness is important to you, then use a const_cast. Then declare a const ref and pass that in.
void Parent::removeChild(const Child *child)
{
QList<Child*>::const_reference constRefToChild = const_cast<Child *> child;
children.removeAll(constRefToChild);
}
The point of this is that if removeAll is ever changed to not take a const, you get a compiler error. Then you'd know that removeAll isn't preserving constness of the argument.