Results 1 to 3 of 3

Thread: QVector, containers, deep copy

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QVector, containers, deep copy

    Hi,
    I've got an wrapper/overload func below. My question: after calling this function, is my data valid? Is a deep copy done?

    How should I do it best? Should I 'new' a QVector or should I ::memcpy()?

    thanks
    K

    Qt Code:
    1. void CLASS::getData(short* piData)
    2. {
    3. QVector<short> trace;
    4. getData(&trace);
    5. piData = trace.data();//do I need to copy this?
    6.  
    7. return;
    8. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QVector, containers, deep copy

    The pointer remains valid as long as the vector isn't reallocated.
    You get a pointer to the actual data, no copy is being done. If you really need a pointer (do you?) then you should make a copy. But I don't see any reason for a need for a persistent pointer other than using some legacy pointer based API.

  3. #3
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector, containers, deep copy

    Qt Code:
    1. void CLASS::getData(short* piData)
    2. {
    3. QVector<short> trace;
    4. getData(&trace);
    5. piData = trace.data();//do I need to copy this?
    6.  
    7. return;
    8. }
    To copy to clipboard, switch view to plain text mode 
    In addition to what Wysota said, piData needs to be a short** or a short&*, or that method is not going to do very much.
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. Do assignment operators in Qt4 return deep or shallow copy?
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 10:01
  2. Copy progress bar
    By safknw in forum Newbie
    Replies: 13
    Last Post: 16th September 2006, 10:02
  3. copy file/s from QFileDialog
    By raphaelf in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 15:26
  4. file copy in Qt 3.3.4 ?
    By npc in forum Newbie
    Replies: 6
    Last Post: 31st March 2006, 15:43
  5. implicit or shared copy?
    By SkripT in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2006, 15:14

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.