使用 luabind 和 std::shared_ptr 进行继承。

我有一个 API(一个特定的 GUI 库),非常依赖于 std::shared_ptr,也就是说经常用作函数参数,并存储在其他对象中。例如,容器小部件(如拆分器和框)将它们的子小部件存储在shared_ptr 中。现在我想通过 Luabind 将此 API 映射到 Lua。在理想情况下,Luabind 将在 shared_ptrs 中创建新对象,并允许我直接将其传递给使用 shared_ptr 参数的函数。对于单个类,例如:

luabind::class_<Button, std::shared_ptr<Button>>("Button")

虽然我这样声明,但我可以公开和使用 void foo(std::shared_ptr<Button> const&) 等函数。

但是,luabind 手册提到为了使用类的层次结构,我必须对层次结构中的所有类使用相同的 shared_ptr 模板实例,例如

luabind::class_<BaseWidget, std::shared_ptr<BaseWidget>>("BaseWidget"),
luabind::class_<Button, BaseWidget, std::shared_ptr<BaseWidget>>("Button")

现在我不能再调用 foo —— 它将无法在 Lua 中找到该函数。我是否可以以某种方式使 luabind 仍然支持使用 shared_ptr 传递按钮?并且,我想知道为什么 luabind 强制要求您在层次结构中的所有类中使用相同的智能指针,而不是它们只是可转换为基类指针。

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

点赞