c++返回多个表,如何知道返回表的名称

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

本文关键字:返回 何知道 c++      更新时间:2023-10-16

我使用Lua来实现一些函数,结果放在表s中并返回到c++代码。例如,在Lua的末尾,我将这些返回给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

您将不知道先前存储表的变量名,但是您的Lua函数将以特定的顺序返回它们。在c++中,这些表是放在堆栈上的,所以courses是上一个值,ages是下一个值。

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