pointer iterator question

J

JoeC

I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?
itr = u.erase(itr);
}else {++itr;}
}
}
 
J

Jim Langston

JoeC said:
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?

if ( (*itr)->marked() ) {
 
R

Rolf Magnus

JoeC said:
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?

if ((*itr)->marked()){
itr = u.erase(itr);

I hope you still have a pointer to your object and delete it somewhere else
in your program.
 
S

Salt_Peter

JoeC said:
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?
itr = u.erase(itr);
}else {++itr;}
}
}

Erasing the element does not recover any allocation that the element
would be storing. So in the case that unit* is a pointer to something
that was newed - you would have a leak.

Any reason why you aren't using a std::vector of smart pointers to
units? Regardless, i fail to see why you would need erase() anyway.
 
J

JoeC

Salt_Peter said:
Erasing the element does not recover any allocation that the element
would be storing. So in the case that unit* is a pointer to something
that was newed - you would have a leak.

Any reason why you aren't using a std::vector of smart pointers to
units? Regardless, i fail to see why you would need erase() anyway.

You are right, I probibly do have a memory leak. I will take this as a
lesson learned. I know the basics of smart pointers and handles but I
just didn't use them here. The context where I learned about them was
with dynamic binding. I was asking this question in the proscess of
writing a game. I do learn from abstract exaples of programs that
really don't do anything but learn much more from larger projects that
I try to implement ideas and solve problems. I current project is a
re-write and expansion of a similliar project and having a smart
pointer class would have been the right way to go, I just didn't have
any problems untile I wanted to "kill" units as a result of combat. It
got copplicated because I have pointers to those pointers in my map
manager and my combat class.

No that you mention it I could have done much better wich making a
class instead of a vector the handle my units. The creation and
destruction would have been much easier and organized.

I am willing to share the game I wrote, although flawed, I am very
proud of it and also I would like some ideas and advice to make it
better for the next time. I often get told not to be so ambitious and
I outstrip my skill. That is the purpose, I want to exploit my
knowlege to the limit and those problems that arise are the focus on
what to work on next. On another topic I wanted to do sothing but I
said I didn't have the experience to do that and the response I got was
the way to get experience is to do it.

I will see if I can create a unit handle that handles the vector of
pointer and controlls the creation usage and destruction of my units
and will work in the current frame work of my game without too much
re-writing. There is a problem when too much modification is needed
that it is often easier to just re-write it all and plug in what you
can than re-writing code that has unintended consequences.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,329
Latest member
InezZ76898

Latest Threads

Top