Question about Pointers and <vectors>
This might be a dumb question but here it goes.
I have a class called T3DLoader it parses an unreal map exported into text file .t3d, in my class i have a vector m_BrushArray. Where BrushActor is a class that holds information about the brush.
ok in my T3DLoader:arseBrushActor function i have something like this.
BrushActor* Brush = new BrushActor();
Brush->m_Name = blah blah
and so on till i get to the end.
Then i push the Brush into the vector like this.
m_BrushArray->push_back(Brush).
ok so heres my question now that Brush is pushed back into the vector is it ok to delete Brush? Since i am about to exit this function, and if i do delete brush does that destroy the one i pushed into the vector?
Any help would be appreciated. Thank You.
If you delete that brush then the pointer stored in your vector will be invalid.
There are some rough edges of using STL containers with pointers, you might want to look at smart pointers. They don''t really make things much easier, just a bit less error-prone in my experience.
There are some rough edges of using STL containers with pointers, you might want to look at smart pointers. They don''t really make things much easier, just a bit less error-prone in my experience.
ok thanks, What if i did something like this in my ParseBrushActor function..
m_BrushArray.push_back( new BrushActor() ) then access it through an iterator?
m_BrushArray.push_back( new BrushActor() ) then access it through an iterator?
either way is fine:
or
they do exactly the same thing. new just creates a space in memory to dump your class and then returns a pointer to that space. a pointer is just a memory address. you aren't "creating" anything extra in the first example that sticks around after the close of the funciton. remember a pointer is just an address. a pointer is NOT the object.
-me
[edited by - Palidine on January 9, 2003 3:07:45 PM]
void makeNewDealio() { Brush * foo = new Brush(); brushVec.push_back(foo);}
or
void makeNewDealio() { brushVec.push_back( new Brush() );}
they do exactly the same thing. new just creates a space in memory to dump your class and then returns a pointer to that space. a pointer is just a memory address. you aren't "creating" anything extra in the first example that sticks around after the close of the funciton. remember a pointer is just an address. a pointer is NOT the object.
-me
[edited by - Palidine on January 9, 2003 3:07:45 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement