我正在具体化我之前的一个问题。
我有两个 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/