Results 1 to 13 of 13

Thread: vector

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector

    Then use the const_cast to get rid of "const" or create your arrays on heap:

    Qt Code:
    1. std::vector <GLfloat*> position;
    2. GLfloat *pos new GLfloat[4];
    3. pos[0] = 0.0;
    4. pos[1] = 1.0;
    5. pos[2] = 0.0;
    6. pos[3] = 0.0;
    7. position.push_back(pos);
    To copy to clipboard, switch view to plain text mode 

    And remember to delete[] the array when you no longer need it to avoid memory leaks.

    Or better yet, make your own class, something like:

    Qt Code:
    1. struct MyData {
    2. MyData(GLfloat d1, GLfloat d2, GLfloat d3, GLfloat d4){ d[0] = d1; d[1]=d2; d[2]=d3; d[3]=d4;}
    3. GLfloat d[4];
    4. GLfloat & operator[](int i){ return d[i]; }
    5. }
    6.  
    7. std::vector<MyData> pos;
    8. pos.push_back(MyData(0.0, 1.0, 2.0, 3.0));
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 27th April 2006 at 22:55.

  2. #2
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: vector

    thanks, the first example compile, but I'm trying to understand better the thing trying const_cast....but don't compile this...
    Qt Code:
    1. GLfloat* p = const_cast <GLfloat[4]> (pos);
    2. //const_cast <GLfloat[4]> (pos); //has it sense?
    3. position.push_back(p); //this don't compile
    To copy to clipboard, switch view to plain text mode 
    Regards

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

    Default Re: vector

    const_cast may not work here or may cause segfaults later on, as you create those arrays on stack and when you push them onto the vector, you'll only push the pointer to the array, which will become invalid when the array goes out of scope. So you should stick with creating those items on heap like in my first example.

  4. The following user says thank you to wysota for this useful post:

    ydz185 (4th May 2006)

Similar Threads

  1. Replies: 4
    Last Post: 29th December 2008, 19:50
  2. vector of vector and computes
    By mickey in forum General Programming
    Replies: 1
    Last Post: 15th May 2008, 12:47
  3. Vector allocation
    By ToddAtWSU in forum General Programming
    Replies: 4
    Last Post: 22nd June 2007, 21:37
  4. insert in a vector of vector
    By mickey in forum General Programming
    Replies: 3
    Last Post: 6th March 2007, 08:45
  5. vector of vector size
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th February 2007, 15: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
  •  
Qt is a trademark of The Qt Company.