Is it ok to dereference like so....

Started by
1 comment, last by Joviex 21 years ago
Yeah, I got class.... class CSprite { vector < CTexture * > Frames; } then we do something nasty in code to get a refernce.... CTexture * Frame = &(*Frames[frameNum]); Is this proper? I typically stay away from the STL but, times change.... From what it looks like though, using a vector of pointers may be a problem no? Won't these suckers get shifted around when I push and pop members? Ergo, how do we keep track of the pointers.... I suppose that makes two questions. "Five passengers set sail that day, for a three hour tour, a three hour tour...." [edited by - Joviex on April 2, 2003 6:02:41 AM]
Advertisement
quote:Original post by Joviex
then we do something nasty in code to get a refernce....

CTexture * Frame = &(*Frames[frameNum]);


I assume that your vector looks like this:

vector<CTexture*> Frames;

If so, writing

CTexture *Frame = &(*Frames[frameNum]);

is the same as

CTexture *Frame = Frames[frameNum];

The operator[] returns a value stored in the vector, which, in your case, is a pointer (not a reference).

quote:
Won't these suckers get shifted around when I push and pop members? Ergo, how do we keep track of the pointers


The order will not change by adding or removing in some uncontrolled manner. If you remove some element, the preceding elements will be shifted by -1 and a push will result in that the preceding elements will be shifted +1. Think of a vector as a dynamic array.

Regards Mats

Edits: '<' and '&qt' + spelling + < and > again...
[edited by - Matsen on April 2, 2003 6:03:12 AM]

[edited by - Matsen on April 2, 2003 6:11:02 AM]

[edited by - Matsen on April 2, 2003 6:14:20 AM]
If you need to push and pop a lot don't use vector, use list (or possibly stack).
It's no problem using pointers in a stl container. Just use
vector<type*>   

instead of
vector<type>   


You can use subscript to access elements in a vector, or iterators. Iterators are more generic and work on ANY stl-container. So they're the preferred way of stepping through your container.


[edited by - Jedyte on April 2, 2003 6:02:19 AM]
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan

This topic is closed to new replies.

Advertisement