Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Solved] [Moved] Can QList<T*> take subclasses of T?
Forum Updated to NodeBB v4.3 + New Features

[Solved] [Moved] Can QList<T*> take subclasses of T?

Scheduled Pinned Locked Moved C++ Gurus
5 Posts 3 Posters 3.2k 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.
  • R Offline
    R Offline
    reactive
    wrote on last edited by
    #1

    Hi, I am having problems understanding why this doesn't compile.

    I have 2 classes, A and B where B publicly inherits A.
    The pseudocode is:

    @
    function(const QList<A*>& list){ ... }

    QList<B*>* list = ...;
    function(*list);
    @

    Error:

    cannot convert parameter 1 from QList<T> to Aconst with T = B

    I'd appreciate any suggestions. Thanks!

    EDIT: please use @-tags for code highlighting, Gerolf

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      You confused in templates and classes.

      B is subclass A
      but QList<B*> is not subclass QList<A*>

      i.e you can:
      @QList<B*>* bList;
      QList<A*>* aList;
      ...
      A *a = bList->at(0);
      aList->append(a);@

      but you can't

      @QList<B*>* bList;
      QList<A*>* aList;
      ...
      aList = bList;@

      --
      Vasiliy

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        See "C++ FAQ 21.3":http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.3 for an explanation and 21.1/21.2 for the background.

        PS:
        I moved this to the C++ guru forum, as it's a general C++ question, but not specifically Qt related.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • R Offline
          R Offline
          reactive
          wrote on last edited by
          #4

          Thanks for your help. I guess I was confused because Java allows this in their generic programming with List<B extends A>. I'm new to Templates but I figured it would work under the Liskov Substitution Principle, since all Bs are As and it's just a pointer (so the size of the object doesn't matter).

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            You have that List<B extends A> in C++ too, you can put all A subclass types into the list, but if you have B extending A you cannot assign a List<B> to a variable of type List<A>. Both are different cases.

            What you've tried does not work in Java either, the following snippet yields an error:

            @
            public InheritanceTest() {
            ArrayList<String> sl = new ArrayList<String>();
            String x = "abc";
            sl.add(x);

            ArrayList&lt;Object&gt; ol = new ArrayList&lt;Object&gt;();
            Integer z = new Integer(1);
            ol.add(z);
            
            ol = sl;
            

            }

            // Error on the last line:
            // Type mismatch: cannot convert from ArrayList<String> to ArrayList<Object>
            @

            http://www.catb.org/~esr/faqs/smart-questions.html

            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