QList::contains and constness
-
Hi all
Inside MainWindow I have an object nodeList of type QList<NodeDescritor*> and I have this simple code (NodeDescriptor is my own class derived from QObject):
@
State MainWindow::getNodeState(const NodeDescriptor *node) const
{
// some code here
if (nodeList.contains(node))
{
// some code here
}
// some code here
}
@During compilation, the invokation of contains() produces the following error: invalid conversion from 'const NodeDescriptor*' to 'NodeDescriptor*' [-fpermissive].
Looking at Qt source code, I discovered the declaration of QList::contains:
@
template <typename T>
Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const
@In My case T is NodeDescriptor*. I think the template should be let's say expanded into the following:
@
bool QList::contains(const NodeDescriptor* &t) const
@For curiosity, I declared a new method with the same signature of contains() within MainWindow:
@
bool test(const NodeDescriptor* &t) const
@@
State MainWindow::getNodeState(const NodeDescriptor *node) const
{
// some code here
if (test(node))
{
// some code here
}
// some code here
}
@An invoking the latter does not produce any error.
I really do not understand why. The two methods have the same signature and are invoked from within the same block statement. As far as I remember, this does not happen for STL list class.
At the moment, I solved the problem using const_cast. But I don't like this solution cause in theory QList::contains should accept const NodeDescriptor *.
Any idea ?
Thanks
Fabio -
I believe the error is caused because the template parameter T that you give as input is const NodeDescriptor* where as the parameter T that contains uses is simply NodeDescriptor*.
To see if it works you can try and change the signature of getNodeState to use NodeDescriptor*