Is it possible to use for a loop with a string macro in C ++?
No no.
Macros are processed before the source code is compiled to create object code.
The values ββof the variables in the for loop are set at runtime. Therefore, they cannot use macros.
It is best to increment the code using an array variable and use the array variable in a for loop.
#define STRING_OBJECT_1 "bird" ... #define STRING_OBJECT_8 "monkey" std::string object_array[] = {STRING_OBJECT_1, ..., STRING_OBJECT_8}; for ( int i = 0; ... ) { do_something(object_array[i]); }
source share