user2505282 user2505282 - 1 year ago 110
C++ Question

C++ Macro to use values of variables

I have a program with 4 objects of a class, say PO1, PO2, PO3, PO4.
I want to call some functions taking these objects as parameters in a for loop.
Like so:

for(int i = 0; i < 4; i++){
func(PO<i>); //Something like a macro to replace the value i in the function.

I tried token pasting, but all I got was POi, instead of getting the value of i.
Is there any way to do this? (Macro, Function, etc.)

Here's my code

Player PO[4] = {Player{'l',WHITE},

'l' and WHITE are the parameters.

Answer Source

Based on @SamVarshavchik's comment, I got a solution.

Here it is,

Player *PK[4] = {&PO1,&PO2,&PO3,&PO4};
for(int i = 0; i < 4; i++}{
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download