Returning tables of objects from C++, adopt policy

发布于 2022-09-06 13:50:27 字数 738 浏览 20 评论 0

Using luabind, I create a table of objects from 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;
}

I register the function as

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

and lua_Object as

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

How can I tell luabind to take ownership of the objects stored in the table ( new lua_Object( ... ) )? What would be a work around?

Thanks -

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2022-09-13 13:50:27

Replace

result[ index ++ ] = o

with

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

On a side note, don't you have to register create_table with raw(_1) policy?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文