【问题标题】:Return multiple tables to C++, how to know the returned table name向C++返回多个表,如何知道返回的表名
【发布时间】: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 值一样,表没有名称。变量有名字。

标签: c++ lua lua-table


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以在所有表​​中都有一个包含其名称/类型的字段。

    【讨论】:

    • 嗯...似乎有问题...看这里是我的代码;
    猜你喜欢
    • 1970-01-01
    • 2013-04-13
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2016-04-27
    • 2019-10-31
    • 2011-05-23
    相关资源
    最近更新 更多