View Full Version : Pointers...
salmanmanekia
7th June 2010, 16:18
Hi.
Can someone shed a a bit og light on the differences between the following
QVector <int*> *rect;
QVector <int> *rect;
QVector <int*> rect;
QVector <int> rect;
Zlatomir
7th June 2010, 16:35
What don't you understand about pointers?
They are not that hard to understand, if you think about "storing" the address of a variable (a good analogy is the address of a house, on an envelope you put the pointer (address) not the actual object(house))
I'll tell you a little about the code you post (but i don't think that will help you understand pointers) so please ask what you don't understand
QVector <int*> *rect; /* rect is a pointer to a QVector <witch stores pointers to ints> carefull with uninitialized pointers, here you don't have the actual QVector, only a pointer*/
QVector <int*> rect; // rect is a QVector <in witch you can add pointers to int>
QVector <int> *rect; // rect is a pointer to a QVector <witch stores integers> carefull with uninitialized pointers... same as first case
salmanmanekia
7th June 2010, 16:49
Thanks for the reply...
In my project i am using QGraphicsView/Scene and Items .Since i have to have many items which i dont know at runtime so i decided to have a vector of it ..
so after this consideration i wrote something like
H_file : QVector <QGraphicsRectItem> rect;
then i tried to setRect the rect like
CPP_file : rect.at(0).setRect(QReal)....
but this throws a runtime error..
I am not good at pointers but i searched for QVector in this forum and found something similar to what i had been doing so i tried to implement that code which was
H_file : QVector <QGraphicsRectItem *> *rect;
CPP_file : rect = new QVector<QGraphicsRectItem>();
and the rect->at(0)->setRect...
but again a run time error follow..
Zlatomir
8th June 2010, 03:48
That is because, this code:
QVector <QGraphicsRectItem> rect; //rect has "place" for 0 elements
create an empty QVector, and you need to use:
QVector <QGraphicsRectItem> rect;
rect.push_back(someInstanceOf_QGraphicRectItem); //this add one element (at the end) to the empty QVector
or
QVector <QGraphicsRectItem> rect(10); //10 is the number of elements, it can grow, with push_back or resize...
rect[0] = someInstanceOf_QGraphicRectItem; //or rect[0].set....
... // this way (operator[]) the position is not checked, so carefull
rect.at(positon) it's for accessing elements, not for set the elements, so it returns the const element at "position" (with check that the position is valid)
QVector reference (http://doc.qt.nokia.com/4.6/qvector.html)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.