Return 多个 tables 到 C++,如何知道返回的 table 名称

Return multiple tables to C++, how to know the returned table name

我正在使用 Lua 来实现一些功能,结果被放入 tables 并且 returned 返回到 C++ 代码.比如Lua结尾,我return这些tables转C++

return names, ages, courses

现在在 C++ 中,我需要读取 tables returned 中的元素。我怎样才能知道 table 的名称,以便我知道要检索哪些元素?换句话说,以下 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

您不知道以前存储表的变量名,但是您的 Lua 函数 returns 它们按特定顺序排列。在 C++ 中,这些表放在堆栈上,因此 courses 是最高值,ages 是下一个值。

您可以在所有表​​中都有一个字段,其中包含它们的 name/type。