   Vectors of Objects vs Pointers
  So I'm diving back into C++ after not touching it, for at least a year, and I have to admit I am rather rusty.  I am having some vector troubles.I have a robot class which contains an ID, and position information.  Whenever I set my vector as a vector of Robot objects as follows:    std::vector<Robot> robotList;    robotList.insert(robotList.begin(), Robot(1));    robotList.insert(robotList.begin(), Robot(2));   std::cout << "There are " << robotList.size() << " robots in the list." << std::endl;   std::vector<Robot>::iterator robotIterator;    for(robotIterator = robotList.begin(); robotIterator != robotList.end(); ++robotIterator) {   Robot temp = *robotIterator;   temp.move();   std::cout << "Robot #" << temp.getID() << " is at position (" << temp.getX() << ", " << temp.getY() << ")." << std::endl;    }    for(std::vector<Robot>::iterator it = robotList.begin(); it != robotList.end(); ++it) {   Robot temp = *it;   temp.move();   std::cout << "Robot #" << temp.getID() << " is at position (" << temp.getX() << ", " << temp.getY() << ")." << std::endl;    }The output is the same for the 1st and 2nd for loop.  In the second loop, the position should be greater then the first for loop because of the move function.  I'm not sure whats going on here, but when I declare it as a vector of pointers like:std::vector<Robot*> robotList;    robotList.insert(robotList.begin(), new Robot(1));    robotList.insert(robotList.begin(), new Robot(2));   std::cout << "There are " << robotList.size() << " robots in the list." << std::endl;   std::vector<Robot*>::iterator robotIterator;    for(robotIterator = robotList.begin(); robotIterator != robotList.end(); ++robotIterator) {   Robot* temp = *robotIterator;   temp->move();   std::cout << "Robot #" << temp->getID() << " is at position (" << temp->getX() << ", " << temp->getY() << ")." << std::endl;    }    for(std::vector<Robot*>::iterator it = robotList.begin(); it != robotList.end(); ++it) {   Robot* temp = *it;   temp->move();   std::cout << "Robot #" << temp->getID() << " is at position (" << temp->getX() << ", " << temp->getY() << ")." << std::endl;    }The output is what it should be.If any of you could refresh me as to what is happening here, that would be great.Thanks,Pedalcat   
  When you use Robot temp = *robotIterator you make a copy of the robot in the vector and then move that copy. Because it's a copy, the original robot isn't affected. Just use robotIterator->move() to move it, and robotIterator->getID(), etc. to get its information.   ;Quote:Original post by pedalcat   Robot temp = *robotIterator;In other words, make a new robot by copying the result of dereferencing the iterator?Quote:temp.move();Then move that copy?EDIT: SIIICRAAAANE!   ;  You could use a reference to keep the same "look" to the code without inducing a copy:for(robotIterator = robotList.begin(); robotIterator != robotList.end(); ++robotIterator) {    // Note the ampersand in the declaration.    Robot &robot = *robotIterator;    robot.move();    std::cout << "Robot #" << robot.getID() << " is at position (" << robot.getX() << ", " << robot.getY() << ")." << std::endl;};  thanks so much, the Robot &temp worked, I vaguely remember all that copy stuff now, thanks for the help.  One last question.  If I wanted to write a game, would it be more efficient to use dynamic memory for something like say a list of aliens in a space invaders type of game?   ;  For space invaders (and nearly all non AAA games) such things would hardly matter for efficiency.In any case, where possible you should avoid dynamic memory. Mostly because it is complex (you now have to deal with lifetime issues etc) but also because it is  more expensive.It depends on what you are doing though. For polymorphism to work, you are usually going to end up using dynamically allocated game objects. For simpler games, you can get away without polymorphism if you want.The most important thing is to write your game in the easiest manner possible. By doing so it will be easy to add cool features and you will have the game finished. Once you have the game finished, if performance is a problem you can tune it then. And the more maintainable the code, the easier it will be to tune.It doesn't make too much sense to talk about the performance of an unfinished program. Its like talking about the top speed of a car that isn't finished being designed or built.   ;  Thanks, that really helps me out.  I didn't realize dynamic memory was more expensive. I will try to avoid it unless I really need it.   ;  Got another problem, trying to test this out with a simple game and I'm getting a seg fault.I have a "player" class that holds a vector of Lasers of the type "Laser" but every time I go to fire a laser, the program seg faults.  I'm not sure if Laser &temp = *LaserIteratorwon't work because its a private member, but tried it as a public member and it didn't work either... ideas?I put everything on pastebin so I wouldn't have a massive post...main.cc http://pastebin.com/m612d8e90player.h http://pastebin.com/m13671622player.cc http://pastebin.com/m383b82fblaser.cc http://pastebin.com/m1c92cb58laser.hhttp://pastebin.com/m4ae16b70   ;Quote:Original post by SneftelEDIT: SIIICRAAAANE!Seriously, SiCrane's a freakin' ninja.  I'm pretty sure he's responsible for half of my ninja'ds.If you step through in the debugger, what line does it seg fault on?   ;  Actually, I'd personally prefer it if you dumped all the code in the post, or at least made the links to the code actual links. But anyways, without you saying which line is giving you trouble, I'm guessing your problem is that GetLaserVector() returns a vector by value, which means that when you call begin on it, the iterator points to something that gets destroyed by the end of the statement. You probably want to return a reference here.   