Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Constructor called while accessing vector

Name: Anonymous 2007-07-05 4:10 ID:gbn8N62F

I have a vector of a class of units:
std::vector<Unit> Units; //list of units

In Unit the constructor Unit::Unit() has a boolean value 'selected'. calling .select sets it to true

but for some reason when i go through each unit in the vector:

for(int i=0;i < Units.size(); i++){
    Units.at(i).select(driver,images);
}

it resets all values as if it got created again, with selected turning false. but
unit.select();

select does change inside the function, but not in the class. why would it do that?

Name: Anonymous 2007-07-05 5:27 ID:xc/kql5U

>>3
There are no pointers in there. What you did was create a new Unit instance called strd, then copy-assigned the one in the vector to strd. That's operator= for you.

Also, why the explicit index?

for (std::vector<Unit>::iterator u = Units.begin(); u != Units.end(); ++u) {
  if (u->hitTest(dragStartX, dragStartY, dragToX, dragToY)) {
    u->select();
  } else {
    u->unselect();
  }
}


You wouldn't have to second-guess yourself with that bounds-checking at() method, plus it's faster because you're not copying your Unit instances unnecessarily.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List