I’m confused about the for each loop in C++. I have this code in a main game loop:
for each (Bubble b in bubbles){
b.Update();
}
for each (Bubble b in bubbles){
b.Draw();
}
It doesn’t update anything, but does draw 1 bubble.. What’s wrong with it?
EDIT: This code works
struct BubbleUpdater {
void operator()(Bubble & b) { b.Update(); }
} updater;
struct BubbleDrawer {
void operator()(Bubble & b) { b.Draw(); }
} drawer;
void OnTimer(){ //this is my main game loop
std::for_each(bubbles.begin(),bubbles.end(),drawer);
std::for_each(bubbles.begin(),bubbles.end(),updater);
}
Change your BubbleUpdater class to accept it’s argument by reference
With that, your call to
std::for_eachshould work.If your compiler supports it (and VC10 does), then you can use lambdas instead of creating a distant function object class. And yes, it’s standard c++, or will be soon enough.