Sign in to follow this  
XiotexStudios

Valid C++?

Recommended Posts

Is this valid C++? std::vector<std::vector<foo*>::iterator> m_vFoo; VS.Net 2003 compiles it but this is the first time I have tried anything like this before...

Share this post


Link to post
Share on other sites
It is valid, but it is a bad idea. Vector iterators become invalid in a few cases, so you shouldn't really be storing them in anything outside of where they were obtained, or you're asking for trouble.

Share this post


Link to post
Share on other sites
In this case its a chapter marker in an event script and is only accessed within the class from which it is defined. The vector itself is only created at one point and destroyed on shutdown without any alterations being made to it throughout its life time.

Basically, what I need is a way of storing chapter points in a list of events. I really didn't want to have to search for those chapters at each access.

Is there (no doubt :)) a more elegant way of handling this, cause I like you are not too happy with the approach I have.

Share this post


Link to post
Share on other sites
Quote:
Original post by XiotexStudios
In this case its a chapter marker in an event script and is only accessed within the class from which it is defined. The vector itself is only created at one point and destroyed on shutdown without any alterations being made to it throughout its life time.

Basically, what I need is a way of storing chapter points in a list of events. I really didn't want to have to search for those chapters at each access.

Is there (no doubt :)) a more elegant way of handling this, cause I like you are not too happy with the approach I have.


Seeing as your vector is storing foo pointers, you could make your other vector use foo pointers.

std:vector<foo*> mainFooVector;
// add 10 elements to mainFoooVector

std::vector<foo*> chapterMarkers;
if( mainFooVector[i]->needToMarkChapter() )
{
chapterMarkers.push_back( mainFooVector[i] );
}





You need to remember to not delete the memory in chapterMarkers, only in mainFooVector. You should of course make sure that before you delete anything from mainFooVector that it is no longer in chapterMarkers.

Share this post


Link to post
Share on other sites
ah, no...

A chapter is a marker of the 'position' into the event list so that I can start the event list from that position - hence why I was storing the iterators to begin with.

so, imagine this script:


Chapter Beginning
SpawnEmitter 3.0 1.0 10 Path_1 0
SpawnEmitter 3.5 1.0 10 Path_2 0

Chapter End

# End of list



By recording the chapters 'Beginning' and 'End' I can move the playhead to the relevant place in the event list.

edit: Actually, that should read 'recording the iterators that point to 'beginning' and 'end'

[Edited by - XiotexStudios on July 20, 2006 2:13:36 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this