But why store pointers if the step class is perfectly value-based? Unless of course you like your programs to leak memory or you like to have more work than required...
But why store pointers if the step class is perfectly value-based? Unless of course you like your programs to leak memory or you like to have more work than required...
Check his original post, he's doing a "new step", which is why QVector<step> didn't work...
I was simply pointing out that he was doing a "new step" in line 3, which requires that the QVector template be QVector<step*>.He can store step values, references, or pointers, but the QVector declaration needs to match.
Not sure what you meant by pointers leaking memory. Pointers don't leak memory, bad programmers who don't know how to clean up pointers do...
jthomps
So he should not be doing "new step" but step(). Don't encourage him to do wrong things.
I mean you have to clear the vector manually after you're done with it (hence "more work") which is not required when you use value-based vectors.Not sure what you meant by pointers leaking memory. Pointers don't leak memory, bad programmers who don't know how to clean up pointers do...
Last edited by wysota; 10th January 2010 at 15:36.
No, I don't. But you should know when to use pointers and when not to use them. Which of the following would you prefer and why?
orQt Code:
void func1() { int x; doSomething(x); }To copy to clipboard, switch view to plain text mode
Qt Code:
void func2() { int *x = new int; doSomething(*x); delete x; }To copy to clipboard, switch view to plain text mode
Normally I use pointers with vectors simply so that if I need to pass an item somewhere I can simply pass a pointer instead so avoid the object being copied for the parameter. However, starting with Qt, I find it easier to use implicitly shared classes, so the data is shared between the object unless it is modified (copy on write strategy).
Of course I'd prefer the first version of the function...There's no benefit to passing around pointers to built in types, but if that were a QByteArray or one of my own classes that is fairly large instead of a int, I'd probably use a new/delete and manage the pointers.
BTW, I just noticed that you're the owner (or admin) for Qt Centre site. Great site and love the recent upgrade. Keep up the good work!
Regards,
jthomps
hi,
it's me again i still got trouble with this vector, and i don't really get your point with what you mean with value-based?
i mean yes the class step is value based, so shouldn't i use pointers? or what you want to tell me?
my c and c++ is quite bad cause i just used java the last 2 years so i may need some generell information about, saving, in this case, steps. :/
Can he? Like this:
???
I would like to be a "Guru"
Useful hints (try them before asking):
- Use Qt Assistant
- Search the forum
If you haven't found solution yet then create new topic with smart question.
Bookmarks