user2505282 user2505282 - 13 days ago 10
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},
Player{'l',WHITE},
Player{'l',WHITE},
Player{'l',WHITE}
};


'l' and WHITE are the parameters.

Answer

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++}{
    func(*PK[i]);
}