Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QList::contains and constness
QtWS25 Last Chance

QList::contains and constness

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fscibilia
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      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*

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fscibilia
        wrote on last edited by
        #3

        Yes! I already did the test you mentioned and it works. Anyway, I want the sign of getNodeState have const NodeDescriptor * as input param type to be sure for the invokers.

        Thanks for your reply

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3685
          wrote on last edited by
          #4

          I'm afraid you don't have many options in this case. I have tried using STL containers and I get the same compiler error.
          Maybe in this case a const_cast is warranted unless you find a better way :)

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved