Results 1 to 4 of 4

Thread: QVector copy constructor

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QVector copy constructor

    Hello,
    I wonder why QList and QVector have a so different behaviour regarding compilation rules. Let us consider the following piece of code :

    Qt Code:
    1. class T
    2. {
    3. public:
    4. T(int i) {};
    5. };
    6. class C
    7. {
    8. public:
    9. C() {};
    10. QList<T> GetTs() {return QList<T>(this->Ts);};
    11. private:
    12. QList<T> Ts;
    13. };
    To copy to clipboard, switch view to plain text mode 

    This code compiles well, but if I change it by replacing QList by QVector:

    Qt Code:
    1. class T
    2. {
    3. public:
    4. T(int i) {};
    5. };
    6. class C
    7. {
    8. public:
    9. C() {};
    10. QVector<T> GetTs() {return QVector<T>(this->Ts);};
    11. private:
    12. QVector<T> Ts;
    13. };
    To copy to clipboard, switch view to plain text mode 

    I get a compilation error:
    Qt Code:
    1. /Library/Frameworks/QtCore.framework/Versions/4/Headers/qvector.h: In member function ‘void QVector<T>::realloc(int, int) [with T = T]’:
    2. /Library/Frameworks/QtCore.framework/Versions/4/Headers/qvector.h:297: instantiated from ‘void QVector<T>::detach_helper() [with T = T]’
    3. /Library/Frameworks/QtCore.framework/Versions/4/Headers/qvector.h:104: instantiated from ‘QVector<T>::QVector(const QVector<T>&) [with T = T]’
    4. error.cxx:10: instantiated from here
    5. /Library/Frameworks/QtCore.framework/Versions/4/Headers/qvector.h:427: error: no matching function for call to ‘T::T()’
    6. error.cxx:4: note: candidates are: T::T(int)
    7. error.cxx:3: note: T::T(const T&)
    To copy to clipboard, switch view to plain text mode 

    The problem is due to the fact there exists no T::T() constructor and I can fix it by adding one:

    Qt Code:
    1. class T
    2. {
    3. public:
    4. T() {};
    5. T(int i) {};
    6. };
    7. class C
    8. {
    9. public:
    10. C() {};
    11. QVector<T> GetTs() {return QVector<T>(this->Ts);};
    12. private:
    13. QVector<T> Ts;
    14. };
    To copy to clipboard, switch view to plain text mode 

    which is not really nice since I would like to prevent users from instanciating class T without explicitely transmitting the integer parameter.
    So could anybody help me fixing the problem:
    - why QList and Qvector do not yield to identical compilation results?
    - is there a better solution, assuming I would like that C::GetTs() returns a copy of table Ts in a very efficient way, which is the reason I would like to make the copy in this way, as explained in the Qt doc ("This operation takes constant time, because QVector is implicitly shared. This makes returning a QVector from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time.")?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QVector copy constructor

    Are you sure you even need QVector? For most purposes, QList is the right class to use. Do you really need continuous memory?
    J-P Nurmi

  3. #3
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QVector copy constructor

    Quote Originally Posted by jpn View Post
    Are you sure you even need QVector? For most purposes, QList is the right class to use. Do you really need continuous memory?
    generally either I process every item in a for loop or I use direct accesses using indices, so I suppose it works quicker on continuous tables...

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QVector copy constructor

    I recommend reading Generic Containers documentation. Pay special attention to "algorithmic complexity".
    J-P Nurmi

Similar Threads

  1. default parameters in constructor class
    By mickey in forum General Programming
    Replies: 4
    Last Post: 23rd February 2008, 19:44
  2. Using setCodecForCStrings(codec) in Constructor
    By danbetz in forum Qt Programming
    Replies: 5
    Last Post: 30th July 2007, 22:23
  3. QDialog: show() and exec() together in constructor?
    By Teuniz in forum Qt Programming
    Replies: 8
    Last Post: 28th February 2007, 12:43
  4. Killing a Window in its constructor
    By hardgeus in forum Qt Programming
    Replies: 6
    Last Post: 15th December 2006, 19:31
  5. use libs under qt4
    By raphaelf in forum Qt Programming
    Replies: 6
    Last Post: 27th February 2006, 18:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.