Using unique dynamic variable names (not variable values!)

前端 未结 4 1678
遇见更好的自我
遇见更好的自我 2020-12-12 01:20

Ok so, this is more a sanity check than anything else.

Lets asume we have a struct called lua_State, now I need to create a uncertain amount of unique lua_State\'s.

相关标签:
4条回答
  • 2020-12-12 01:47

    Use a std::vector to both store the created states and generate sequential identifiers (i.e. array indices). Unless I'm missing something, then you are grossly over-complicating your requirements.

    std::vector<lua_State *> stateList;
    
    // create a new Lua state and return it's ID number
    int newLuaState()
    {
        stateList.push_back(luaL_newstate());
        return stateList.size() - 1;
    }
    
    // retrieve a Lua state by its ID number
    lua_State * getLuaState(int id)
    {
        assert(0 <= id && stateList.size() > id);
        return stateList[id];
    }
    
    0 讨论(0)
  • 2020-12-12 01:57

    Why do you need a variable name for every index? Why is it not good enough to refer to, for example, luaMap[0] and luaMap[1]? I don't think there's really any way to do what you want. You need some sort of dynamic array, like a std::vector.

    0 讨论(0)
  • 2020-12-12 02:01

    GiNaC does this, but the name has to be explicitly given to a variable

    0 讨论(0)
  • 2020-12-12 02:04

    Can't you use std::map<std::string, lua_State*> and use the script name as an index to the state?

    0 讨论(0)
提交回复
热议问题