返回多个表格给C++,如何知道所返回的表格名称

我正在使用Lua来实现一些函数,结果被放在s表中并返回给C++代码。例如,在Lua的最后,我将这些返回给C++。

return names, ages, courses

现在在C++中,我需要读取返回的表中的元素。我如何知道表的名称,以便我知道要检索哪些元素?换句话说,以下 sudocode 阐述了我想要做的事情:

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。

点赞
用户2546626
用户2546626

你不会知道之前表格存储的变量名称,但是你的 Lua 函数会按照特定顺序返回它们。在 C++ 中,这些表格被放在堆栈上,所以 courses 是顶部值,而 ages 是下面的值。

2015-10-16 08:41:39
用户3052941
用户3052941

你可以在所有的表格中设置一个字段,以包含它们的名称/类型。

2015-10-16 08:46:31