使用唯一的动态变量名称(不是变量值!) [英] Using unique dynamic variable names (not variable values!)

查看:244
本文介绍了使用唯一的动态变量名称(不是变量值!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这比其他任何东西都更合理的检查。



由于我们有一个名为lua_State的结构,现在我需要创建一个不确定的唯一lua_State的。为了确保我不使用相同的变量名两次,我需要有某种方式来获得一个唯一的变量每次我做一个新的状态。



但是,只有一种方法(我想?)创建一个新的状态,如下:

  lua_State * S = lewL_newstate(); 

现在我需要一些方法来动态改变S >

例如:如果我有4个lua文件,并且我想将每个加载到自己的lua_State,我会调用:lua_State * A = lewL_newstate();对于第一个,lua_State * B = lewL_newstate();为第二,等等。请记住,lua文件的数量各不相同,因此创建固定数量的状态可能不会很好。



我该如何做? >

说明:



.h

 code> struct lua_State 

.cpp

  createNewState(Lua_State * something){
lua_State * something = luaL_newstate();
}

我想过创建一个

  std :: map< int,lua_State *> luaMap; 

但是我还是会遇到一个问题:实际生成一个变量名对于每个int-index。



所以,我喝了太多的咖啡,并有一个明显的简单的解决方案,我想要做的,或者我应该



提前感谢。

解决方案

p>使用 std :: vector 来存储创建的状态并生成顺序标识符(即数组索引)。

  std :: vector< lua_State *>如果没有错误, stateList; 

//创建一个新的Lua状态并返回ID号
int newLuaState()
{
stateList.push_back(luaL_newstate());
return stateList.size() - 1;
}

//通过ID号检索Lua状态
lua_State * getLuaState(int id)
{
assert(0 <= id &&& stateList.size()> id);
return stateList [id];
}


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. To make sure I don't use the same variable name twice I would need to have some sort of way to get an unique variable every time i make a new state.

However there is only one way (I think?) to create a new state, and that is as follows:

lua_State *S = lewL_newstate();

Now I would need some way to dynamically change that "S" to.. whatever.

For example: If I had 4 lua files, and I wanted to load each into their own lua_State, I would call: lua_State *A = lewL_newstate(); for the first, lua_State *B = lewL_newstate(); for the second, and so on. Keep in mind the number of lua files varies so creating a fixed number of states probably won't go over well.

How would I go about doing this?

clarification:

.h

struct lua_State

.cpp

createNewState(Lua_State* something){
 lua_State* something = luaL_newstate();
}

I thought about creating a

std::map<int, lua_State*> luaMap;

but then I would still have the problem of actually generating (for lack of better words) a variable name for every int-index.

So, have I been drinking too much coffee and is there a glaringly obvious simply solution to what I am trying to do, or should I just stop coding untill the crazy blows over?

Thanks in advance.

解决方案

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];
}

这篇关于使用唯一的动态变量名称(不是变量值!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆