Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Joviex

Is it ok to dereference like so....

This topic is 5655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!