目前我正在试验 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 的源代码,遗憾的是没有成功。我也尝试重建这种行为,但也没有成功。
那么,我是否遗漏了一些非常明显的东西?
提前致谢!
最佳答案
-
luabind::module
是一个函数,它返回类型luabind::module_
,它有一个重载的[]
运算符接受luabind::scope
类型的参数. -
luabind::class_
是一个类,它有一个类型为const char*
的构造函数和一个成员函数def
返回class_&
所以调用def
可以链接。 -
luabind::class_
派生自一个名为luabind::detail::class_base
的类,源自luabind::scope
,所以最后的class_
返回可以转换为scope
并作为参数传递给luabind::module_::operator[]
.
关于c++ - 类似 Luabind 的语法(索引运算符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38317923/