1

I try to build dynamic string from counter variable in loop in c++ , I was thinking about simple macro.

something like this :

// its enum element string LOCAL_1, LOCAL_2 .....
    #define CREATE_STR(N) LOCAL_##N

    int ii =1;
        for(;ii<=COLS_NUMBERS_PER_WINDOW;)
        {
                     pGameObj->IniGameObj(pMainWindowObjCenter,
                                        CREATE_STR(ii),
                                        Z,                                   
                                        ii);
            GameVector.push_back(pGameObj);
            ii++;
        }

but it gives me compiler error

error C2065: 'LOCAL_ii' : undeclared identifier

Is there any other good way to build dynamic string from dynamic variable? I can use c++11.

UPDATE:
i think i just need this :
Enum C++ Get by Index

Community
  • 1
  • 1
user63898
  • 29,839
  • 85
  • 272
  • 514
  • Macros run before "normal" compilation, let alone during runtime. – chris Feb 26 '14 at 13:52
  • I'm hopping that this is what you were referring to. http://stackoverflow.com/questions/806543/c-macros-manipulating-a-parameter-specific-example – Roger Rabbit Feb 26 '14 at 13:57
  • @chris: im getting : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Alloc>' to 'GameObjectTypeLocation' its enum type – user63898 Feb 26 '14 at 13:58

2 Answers2

4

It's hard to tell what you're trying to do. But I'm guessing that you have something like enum { LOCAL_1, LOCAL_2, ..., and you want to convert ii to these symbols.

You definitely can't do this with the preprocessor. The value of ii is not even known at compile time, and because the preprocessor happens even before that, the best you can do is a simple text substitution, which makes it impossible to use ii as a changing expression like you want.

You have to do the expansion at runtime, like @Jarod42 suggests ("LOCAL_" + std::to_string(ii)), but then you're left with a string. At runtime, the compiler has since forgotten about symbols like LOCAL_1.

So back to square 1, how to convert ii to a LOCAL_xx? I would suggest the following:

If LOCAL_xx is an enum, then just assign the enum constants to the value you want to convert from ii.

enum {
    LOCAL_1 = 1,
    LOCAL_2 = 2,
};

Now, there is basically no difference between ii and LOCAL_xxx. No conversion is necessary.

If LOCAL_xx are not integer constants, then you will need to construct the mapping yourself. For example using std::map<int, LOCAL_xxx> or a big switch statement.

tenfour
  • 36,141
  • 15
  • 83
  • 142
0

You may use something like

const GameObjectTypeLocation locals[COLS_NUMBERS_PER_WINDOW] = {
    LOCAL_1, LOCAL_2, LOCAL_3
};
for (int ii = 1; ii <= COLS_NUMBERS_PER_WINDOW; ++ii)
{
    pGameObj->IniGameObj(pMainWindowObjCenter, locals[ii - 1], Z, ii);
    GameVector.push_back(pGameObj);
}

if LOCAL_1, LOCAL_2 follow a logic, you may convert the int into the enum directly.

for (int ii = 1; ii <= COLS_NUMBERS_PER_WINDOW; ++ii)
{
    // Assuming LOCAL_1 = 0, LOCAL_2 = 1, ...
    pGameObj->IniGameObj(pMainWindowObjCenter, GameObjectTypeLocation(ii - 1), Z, ii);
    GameVector.push_back(pGameObj);
}

As a note, with:

enum GameObjectTypeLocation { LOCAL_1, LOCAL_2};

LOCAL_1 is 0, LOCAL_2 is 1, ..

Jarod42
  • 203,559
  • 14
  • 181
  • 302