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. QVector::append() and copy-constructor
QtWS25 Last Chance

QVector::append() and copy-constructor

Scheduled Pinned Locked Moved Solved General and Desktop
qvectorcopy-constructo
8 Posts 6 Posters 7.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.
  • D Offline
    D Offline
    dream_captain
    wrote on 5 Oct 2017, 05:47 last edited by
    #1

    Can anybody explain why copy-constructor is being called twice here?

    MyClass.h

    #include <QDebug>
    class MyClass
    {
    public:
        MyClass(int a) :
            mA(a)
        {
            qDebug() << "parameterized constructor";
        }
    
        MyClass()
        {
            qDebug() << "default constructor";
        }
    
        MyClass(const MyClass &other)
        {
            qDebug() << "copy-constructor" << other.mA;
        }
    
    private:
        int mA;
    };
    

    main.cpp

    #include <MyClass.h>
    ...
     MyClass myclass(10);
     QVector<MyClass> qvec;
     qvec.append(myclass);
    ...
    

    Output:

    parametrized constructor
    copy-constructor  10
    copy-constructor  19182872
    
    T 1 Reply Last reply 5 Oct 2017, 06:20
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Oct 2017, 06:13 last edited by mrjj 10 May 2017, 06:14
      #2

      Hi

      qvec.append(myclass);

      This will copy it to the one/new spot in the list
      First new spot in list is created , then your class is copied to it.

      for std::vector they made emplace to solve that
      http://www.cplusplus.com/reference/vector/vector/emplace/

      we say its constructed in place and hence no copy is needed.

      I do not know if QVector can do the same.

      1 Reply Last reply
      0
      • D dream_captain
        5 Oct 2017, 05:47

        Can anybody explain why copy-constructor is being called twice here?

        MyClass.h

        #include <QDebug>
        class MyClass
        {
        public:
            MyClass(int a) :
                mA(a)
            {
                qDebug() << "parameterized constructor";
            }
        
            MyClass()
            {
                qDebug() << "default constructor";
            }
        
            MyClass(const MyClass &other)
            {
                qDebug() << "copy-constructor" << other.mA;
            }
        
        private:
            int mA;
        };
        

        main.cpp

        #include <MyClass.h>
        ...
         MyClass myclass(10);
         QVector<MyClass> qvec;
         qvec.append(myclass);
        ...
        

        Output:

        parametrized constructor
        copy-constructor  10
        copy-constructor  19182872
        
        T Offline
        T Offline
        Taz742
        wrote on 5 Oct 2017, 06:20 last edited by Taz742 10 May 2017, 06:21
        #3

        @dream_captain
        i dont know what happens... but i noticed.
        if i call constructor for example

            MyClass myclass(10);
            QVector<MyClass> qvec;
            qvec.push_back(MyClass(myclass));
        
            parameterized constructor
            copy-constructor 10
            copy-constructor 1960650240
            copy-constructor 10419688
        
            MyClass myclass(10);
            std::vector<MyClass> qvec;
            qvec.push_back(myclass);
        
            parameterized constructor
            copy-constructor 10
        

        Do what you want.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 5 Oct 2017, 07:08 last edited by
          #4

          @dream_captain: If your class is movable [1], than you can use std::move from C++11 to optimize:

          int main()
          {
              MyClass myclass(10);
              QVector<MyClass> qvec;
              qvec.append(std::move(myclass));
          }
          

          which gives the following output:

          parameterized constructor
          copy-constructor 10
          

          VoilĂ  :)

          Note that the most Qt types are implicitely shared, so even calling the copy constructor is very cheap for them (effects for example QStringList::append())

          [1] https://stackoverflow.com/questions/3106110/what-are-move-semantics

          Qt has to stay free or it will die.

          1 Reply Last reply
          4
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 5 Oct 2017, 07:25 last edited by
            #5

            Hi,

            Because when you add custom copy constructor you "destroy" the implicitly created move constructor/copy assignment operator.

            Modify your class like that:

            class MyClass
            {
            public:
                MyClass(int a) :
                    mA(a)
                {
                    qDebug() << "parameterized constructor";
                }
            
                MyClass() :
                    mA(0)
                {
                    qDebug() << "default constructor";
                }
            
                MyClass(const MyClass &other) :
                 mA(other.mA)
                {
                    qDebug() << "copy-constructor" << mA;
                }
            
                MyClass(MyClass &&) = default;
                MyClass & operator= (const MyClass &) = default;
            
            private:
                int mA;
            };
            

            And you should be good again.

            See C++ move constructor and C++ copy assignment operator.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            6
            • D Offline
              D Offline
              dream_captain
              wrote on 5 Oct 2017, 09:50 last edited by
              #6

              @SGaist it doesn't work with old visual studio 2012 compiler and Qt 5.5.1, but works well with vs2015 and qt 5.8.0. Seems like std::move support in msvc2012 is an issue.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 5 Oct 2017, 19:04 last edited by
                #7

                Because VS2012 has incomplete C++11 support. See this page.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3
                • C Offline
                  C Offline
                  cdietz
                  wrote on 17 Jul 2018, 09:10 last edited by
                  #8

                  Related issue: https://bugreports.qt.io/browse/QTBUG-49250

                  1 Reply Last reply
                  2

                  • Login

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