【问题标题】:Luabind-like syntax (index operator)类 Luabind 语法(索引运算符)
【发布时间】:2016-11-14 01:10:15
【问题描述】:

目前我正在试验 Luabind-Library,我偶然发现了它的调用语法。它的行为和工作方式与预期的一样,但不知何故我无法理解它为什么或如何做。
有问题的代码如下:

Class Animation
{
    std::vector frames;
public:
    Animation(){}
    ~Animation(){}
    addFrame(const Texture2D *in_image);
};

//Somewhere else
luabind::module(LuaState)
[
 luabind::class_("Animation")    // < "Animation" how we want to name the Class in the Lua runtime
 .def(luabind::constructor<>())             // < Binds the empty constructor
 .def("addFrame", &Animation::addFrame)     // < Binds the &Animation::addFrame method to lua with the name "addFrame"
];

更具体地说,我不明白方括号中发生了什么。为什么这行得通?我试图阅读 Luabind 的源代码,遗憾的是没有成功。我也尝试重建这种行为,但也没有成功。
那么,我是否遗漏了一些非常明显的东西?

提前致谢!

【问题讨论】:

    标签: c++ c++11 lua luabind


    【解决方案1】:
    • luabind::module 是一个函数,它返回 luabind::module_ 类型,它有一个重载的 [] 运算符,接受 luabind::scope 类型的参数。
    • luabind::class_ 是一个类,它有一个采用 const char* 类型的构造函数和一个返回 class_&amp; 的成员函数 def,因此可以链接对 def 的调用。
    • luabind::class_ 派生自一个名为luabind::detail::class_base 的类,该类派生自luabind::scope,因此返回的最终class_ 可以转换为scope 并作为参数传递给luabind::module_::operator[]

    【讨论】:

    • 非常感谢,正是我需要的!
    猜你喜欢
    • 1970-01-01
    • 2012-01-25
    • 2012-05-06
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    相关资源
    最近更新 更多