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.
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]);
}
Use array or std::vector
instead
MyObject Pos[4] = {MyObject{42}, MyObject{51}, MyObject{21}, MyObject{12}};
for (auto& po : Pos) {
func(Po);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.