I am designing a artificial life simulator atm and it would be very useful if I could pass any statement as an argument. For Example.

LoopStatement(X += 2,50)//loop statement 50 times
//or
LoopStatement(X += 2 * Speed,75)//loop statement 75 times

I know the for loop has something like that. Also how would use it?
Any help would be appreciated links or explanations.

You can not pass an expression to a function, since the expression would be evaluated right before the function is called, and you would not be able to repeat it.

On the other hand, you can indeed use a for loop. Hopefully that's what you're looking for.

for ( int i=0; i < 50; X += 2, i++ ) {}; /* Perform X+=2 50 times */

You can not pass an expression to a function, since the expression would be evaluated right before the function is called, and you would not be able to repeat it.

On the other hand, you can indeed use a for loop. Hopefully that's what you're looking for.

for ( int i=0; i < 50; X += 2, i++ ) {}; /* Perform X+=2 50 times */

lol not quite but thank you anyways.

Try something like this :

void doSomething(const int updateValue, const int maxLoop){
  if(updateValue < 0 ) return;
  for(int curr = 0; curr != maxLoop; curr += updateValue){
   /* do something */
  }
}

and call it like so :

int main(){
  //stuff
   whileGameIsNotOver(){
     doSomething( player.updateSpeed(), Wall.MaxPosition());
     doSomething(2,50);  //loop statement 50 times, with increments of 2.
     doSomething(2*enemy.speed(),75);
  }
 //stuff
}

Lambda functions might be what you're looking for. In Python these are part of the language. In C++ there is an implementation in the Boost library.

Have a look at
http://www.boost.org/doc/libs/1_42_0/doc/html/lambda.html

Hope this helps.

I have also written various alife simulators in C++. Would be interested to hear more about what you are doing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.