Results 1 to 4 of 4

Thread: Better way to initialize QVector

  1. #1
    Join Date
    May 2009
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Better way to initialize QVector

    I guess my question is more about C++ then Qt, but I'm not sure.

    I have a class, and it has a member that is a QVector<type>

    I want to initialize it inside the Class Constructor. After some tries, I got one working:

    variableName = QVector<type>::QVector<type>(0);

    Qt Code:
    1. .h
    2. class Protocol
    3. {
    4. ...
    5. typedef int second;
    6. typedef QVector<second> conj_seconds;
    7. ...
    8. Private:
    9. conj_seconds list_seconds;
    10. ...
    11. }
    12.  
    13.  
    14. .cpp
    15. Protocol::Protocol()
    16. {
    17. ...
    18. list_seconds = QVector<second>::QVector<second>(0);
    19. ...
    20. }
    To copy to clipboard, switch view to plain text mode 

    Is there a more... efficient way? elegant way? It seems so... brute to me

    and,
    another question,
    I can't seem to be able to use "conj_seconds" as a return parameter for my methods. Any ideas why?
    (yes, I included "protocol.h", and <QVector>)

    thank you!

    *Edit*
    actually, No typedef name can be used as a return parameter, but they work fine as other parameters...
    Last edited by lyucs; 20th August 2009 at 15:46.

  2. #2
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Better way to initialize QVector

    Why would you want to initialize your container? Just forget about it you don't need to do that. And your second question, put typedef's outside of the class.

  3. The following user says thank you to RSX for this useful post:

    lyucs (24th August 2009)

  4. #3
    Join Date
    May 2009
    Posts
    17
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Better way to initialize QVector

    Quote Originally Posted by RSX View Post
    Why would you want to initialize your container? Just forget about it you don't need to do that. And your second question, put typedef's outside of the class.
    I.... don't? ookay, then

    It worked.

    thanks!

  5. #4
    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: Better way to initialize QVector

    You can always do it like this:
    Qt Code:
    1. Protocol::Protocol() : list_seconds(0) { ... }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 1st May 2009, 07:35
  2. QVector copy constructor
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2008, 17:52
  3. Problem with qvector
    By zorro68 in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2008, 01:02
  4. QVector and QSetting
    By mattia in forum Newbie
    Replies: 1
    Last Post: 8th November 2007, 16:21
  5. QVector
    By sabeesh in forum Qt Programming
    Replies: 2
    Last Post: 17th September 2007, 14:37

Tags for this Thread

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.