c++ - 关于类(class)返回另一个类(class)的 SWIG-Lua 问题

标签 c++ lua swig

我正在具体化我之前的一个问题。

我有两个 C++ 类,我使用 SWIG 来包装它们。一个类中的方法可以返回指向另一个类的指针。我怎样才能让 Lua 将其视为不仅仅是用户数据?

更具体地说:

我有

class fruit
{
     int numberofseeds;
  //some other stuff about fruit constructors etc...
   public:
     getseedcount()
     {
        return numberofseeds;
     }
}

class tree
{
    fruit * apple; 
    public:
      //constructors and whatnot
    fruit * getfruit()
    {
         return apple;
    }

}

我用 SWIG 包装了这两个类,这样我就可以在 Lua 中访问它们

所以我可以在 Lua 中获取对象 x=pomona.tree(grannysmith)

我现在的问题是:我怎样才能安排事情以便在我键入 y=x:getfruit() 时得到一个 pomona:fruit 类型的对象?我在哪里可以写一些行 y:getseedcount()? 目前我得到的只是不可食用的用户数据。

最佳答案

如果您的 SWIG .i 文件设置正确,您可以使用“:”运算符:

local y = x:getfruit()
local z = y:getseedcount()

参见 SWIG Lua documentation 的“类”部分 (23.2.7) .

如果这不起作用,您需要告诉 SWIG 如何使用 .i 文件中的类型映射将 fruit* 输出参数转换为 Lua 表示形式。像这样的东西:

%typemap(out) fruit*
{
    swig_module_info* module = SWIG_GetModule(L);
    swig_type_info* typeInfo = SWIG_TypeQueryModule(module, module, "fruit *");

    SWIG_NewPointerObj(L, $1, typeInfo, 1);
}

关于c++ - 关于类(class)返回另一个类(class)的 SWIG-Lua 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2862398/

相关文章:

C++ 分数混合比较运算符错误

C++.eof。未读取文件的最后一个值

lua - 在Lua中,是否有一个函数可以告诉我我正在运行的当前版本?

python - 将 .lua 表转换为 python 字典

适用于 Windows 的 Python SVN 绑定(bind)

python - 如何使用 SWIG 让 python 切片与我的 c++ 数组类一起工作

c++ - 管理前向声明

c++ - 负数字字符串(例如 "-10")到 unsigned short

lua - 将数据位置信息添加到 latex 输出

c++ - swig 和 C++11 兼容性