Luabind:将派生类作为参数

最近我一直在学习 Luabind,打算将其集成到我们的软件中。我遇到了一些问题,研究了 Rasterbar Software 的 Luabind 文档,但仍无法解决。基本上,我正在暴露一个函数,该函数将一个字符串和一个抽象基类作为参数。首先,我不确定是否我在正确的方法上进行操作,或者可能需要在 lua 中进行一些特殊处理才能使其正常工作。无论如何,以下是代码:

class UIFactory
{
    void addComponentFactory(std::string name, BaseFactory* factory);
}

BaseFactory 是一个抽象基类,返回一个 UIComponent(按钮、文本等),我们有一个名为 TemplateFactory 的派生工厂,可以这样实例化…

TemplateFactory<Button> buttonFactory = new TemplateFactory<Button>();

然后我们将这些传递给 C++ 中的 UIFactory,像这样...

uiFactory.addComponentFactory("Buttons", buttonFactory);

在 luabind 中...

module(state)
[
    class_<UIFactory>("UIFactory")
    .def(constructor<>())
    .def("AddFactory", &UIFactory::addComponentFactory)
];

在 lua 中...

uiFactory = UIFactory()

buttonFactory = ButtonFactory()

uiFactory:AddFactory("Button", buttonFactory)

最后一行没有执行,我已经检查了按钮工厂和 UI 工厂是否已被创建,它们确实已被创建。我是否遗漏了什么?

非常感谢您的帮助。

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

点赞
stackoverflow用户1301322
stackoverflow用户1301322
原本的 markdown 格式:

luabind::module(state) [ luabind::class_("BaseFactory") ];

luabind::module(state) [ luabind::class<TemplateFactory

2012-04-10 08:20:37