【发布时间】:2015-10-16 16:46:31
【问题描述】:
我正在使用 Lua 实现一些功能,结果放在 tables 中并返回到 C++ 代码。例如,在 Lua 结束时,我将这些 tables 返回到 C++
return names, ages, courses
现在在 C++ 中,我需要读取返回的表中的元素。如何知道表的名称以便知道要检索哪些元素?换句话说,以下 sudo 代码说明了我想要做什么:
if table_name == "names": //some commands can realize this?
lua_getfield(L, -1, "Tom");
the_name = lua_tostring(L, -1);
cout << the_name << endl;
Lua_pop(L, 1);
elif table_name == "ages": //similar to last comment...
lua_getfield(L, -1, "girls");
the_age = lua_tostring(L, -1);
....... //some operations
有人知道吗?顺便说一句,我在 win7 上使用 Lua5.3.1
【问题讨论】:
-
您可以在所有表中都有一个字段,其中包含它们的名称/类型。
-
@malhotraprateek 听起来不错!
-
我会添加它作为答案。
-
像所有 Lua 值一样,表没有名称。变量有名字。