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. Problems with QList and const Class

Problems with QList and const Class

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k Views 1 Watching
  • 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.
  • VinorcolaV Offline
    VinorcolaV Offline
    Vinorcola
    wrote on last edited by
    #1

    Hello, I have a problem when using QList<MyCLass*>::contains() and QList<MyClass*>::indexOf() with const instances.

    Here is my example code :

    @#ifndef MAINWINDOW_HPP
    #define MAINWINDOW_HPP

    #include <QList>
    #include <QMainWindow>

    #define ERROR -9

    class MyClass
    {
    private:
    QString m_name;

    public:
        MyClass() :
            m_name("Default") {}
    

    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

        QList<MyClass*> m_list;
        
    public:
        MainWindow() :
            QMainWindow(),
            m_list() {}
        
        
        
        int getIndexOf(const MyClass* myClass) const
        {
            if (m_list.contains(myClass))
            {
                return m_list.indexOf(myClass);
            }
            else
            {
                return ERROR;
            }
        }
    

    };

    #endif // MAINWINDOW_HPP
    @

    When compiling, I have those errors:
    MainWindow.hpp:40: erreur : invalid conversion from 'const MyClass*' to 'MyClass*' [-fpermissive]
    qlist.h:886: error: initializing argument 1 of 'bool QList<T>::contains(const T&) const [with T = MyClass*]' [-fpermissive]
    MainWindow.hpp:42: erreur : invalid conversion from 'const MyClass*' to 'MyClass*' [-fpermissive]
    qlist.h:853: error: initializing argument 1 of 'int QList<T>::indexOf(const T&, int) const [with T = MyClass*]' [-fpermissive]

    I do not realy understand why because those methodes are expected constant template :
    QList::contains(const T&)
    and here, T = MyClass* and I send a const MyClass* in parameter.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      There's a difference between a constant pointer to MyClass and a pointer to a constant MyClass. The getIndexOf Method has the latter kind as parameter, the QList methods expect the first kind.

      Look:
      int a;
      someList.contains(a);
      the "const T&" of QList::contains guarantees, that a won't be changed.

      Similarly:
      MyClass *a;
      someList.contains(a);
      guarantees, that a won't be changed. a, the pointer value (32/64-bit number). It's very different from const MyClass *a, which guarantees that *a, the object the 32/64-bit number points to, won't be changed.

      Solution: try const_cast, or change the list type to const MyClass*

      //EDIT: Something to think about along those lines:
      @int i;
      QList<const int> a;
      QList<const int*> b;
      a << i;
      b << &i;
      @
      What will work, what will fail? Which list can/can't do what?

      1 Reply Last reply
      0
      • VinorcolaV Offline
        VinorcolaV Offline
        Vinorcola
        wrote on last edited by
        #3

        Ok,
        I understand the problem. Thanks for your help.

        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