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. Casting from QList<InheritedFromQObject> to QList<QObject>
QtWS25 Last Chance

Casting from QList<InheritedFromQObject> to QList<QObject>

Scheduled Pinned Locked Moved Unsolved General and Desktop
qobjectstaticcastcasting
4 Posts 2 Posters 4.7k 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.
  • K Offline
    K Offline
    Kofr
    wrote on 21 Oct 2015, 18:26 last edited by Kofr
    #1

    Hi guys,
    I have a problem when casting:

    /home/kofr/dev/shit/Converter/converter.cpp:98:

    error: invalid static_cast from type 'QList<Unit*>*' to type 'QList<QObject*>* ' 
    
    sourceUnitDataModel = static_cast<QList<QObject *> *>((&modelsByCategory[category]));
                                                                                         
    

    Class Unit is the inherited from QObject class. What might be a problem?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 21 Oct 2015, 18:36 last edited by Chris Kawa
      #2

      You can't cast any container<A> to container<B>. Template class types are unrelated, even if T in them has something in common. There might be specializations, some sizes may differ, alignment may vary or buncha other things. Yours is just a special case but these types are just not compatible in general. For compiler it's basically the same as converting vector<int> to vector<string>.

      The correct way to do what you want is to create another container and copy the elements casting them one at a time, e.g. using std::transform.

      K 1 Reply Last reply 21 Oct 2015, 18:40
      2
      • C Chris Kawa
        21 Oct 2015, 18:36

        You can't cast any container<A> to container<B>. Template class types are unrelated, even if T in them has something in common. There might be specializations, some sizes may differ, alignment may vary or buncha other things. Yours is just a special case but these types are just not compatible in general. For compiler it's basically the same as converting vector<int> to vector<string>.

        The correct way to do what you want is to create another container and copy the elements casting them one at a time, e.g. using std::transform.

        K Offline
        K Offline
        Kofr
        wrote on 21 Oct 2015, 18:40 last edited by
        #3

        @Chris-Kawa thx.
        I actually cast pointers to containers. does this fact make any difference??

        C 1 Reply Last reply 21 Oct 2015, 18:46
        0
        • K Kofr
          21 Oct 2015, 18:40

          @Chris-Kawa thx.
          I actually cast pointers to containers. does this fact make any difference??

          C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 21 Oct 2015, 18:46 last edited by
          #4

          The two list types are unrelated so casting pointers to them is still not ok at all. To push the previous analogy - for compiler it's like casting int* to string*. You can of course use reinterpret_cast and the compiler will be happy to trust you, but that is in no way correct and will obviously explode in your face at runtime.

          1 Reply Last reply
          2

          1/4

          21 Oct 2015, 18:26

          • Login

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