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 copy constructor

QList copy constructor

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 8.2k 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    If I have the following code:

    @QList<X*> l1;
    QList<X*> l2 = new QList<X*>(l1);@

    Where X is some kind of class.
    Will the copy constructor of QList automatically copy all its elements (by calling the elements copy constructor), or will just the pointers e copied(shallow copy).

    This means, will QList copy contructor automatically call the copy constructor of X (if present)?

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

      QList calls the copy constructor of the template type. That type is "X*" = "pointer-to-X" (not "X"!), so the copy constructor of "pointer-to-X" is called (not a copy constructor of "X"), which just copies the value of the pointer (aka address of the pointed-to object). It neither does copy the referenced object nor calls any copy constructor of type X.

      So, to make it short: the list elements are not copied. The elements in both lists point to the very same objects.

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

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

        Ok, thank you.

        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