C++ 中返回对象表,采用深度拷贝策略。

使用luabind,在C ++中创建一个对象表

luabind::object create_table(lua_State *L)
{
  luabind::object result = luabind::newtable(L);
  int index = 1;
  for ( ... ) {
    lua_Object *o = new lua_Object( ... );
    result[ index ++ ] = o;
  }
  return result;
}

我将该函数注册为

module(L)
[
  def("create_table", &create_table)
]

并将lua_Object注册为

class_<lua_Object> reg("Object");
reg
  .def(constructor<float,float>())
  ;
module(L) [ reg ];

如何告诉luabind获取存储在表中的对象(new lua_Object(...))的所有权? 有什么解决方法?

谢谢 -

原文链接 https://stackoverflow.com/questions/4932429

点赞
stackoverflow用户169828
stackoverflow用户169828

将以下代码替换为新代码,并保留原始的markdown格式:

result[ index ++ ] = o

新代码如下:

result[ index ++ ] = luabind::object(L, o, luabind::adopt(luabind::result));

此外,你不需要使用raw(_1)策略来注册create_table吗?

2011-02-08 14:22:29