c++ - 类似 Luabind 的语法(索引运算符)

标签 c++ c++11 lua luabind

目前我正在试验 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/

相关文章:

c++ - 使用weak_ptr的观察者模式

c - 在 Lua 之外使用 Lua 的哈希表是否可能/实用?

c++ - wxWidgets中的 'delete'和 'destroy'有什么区别?

c++ - 如何使用 umdh.exe 在所有堆中跟踪内存泄漏?

c++ - 函数的嵌套 C++ 模板参数

c++ - 线程事件会在程序退出后自动关闭吗?

c++ - 我可以升级 Xcode 以支持更新版本的 GCC 来学习 C++0x 吗?

c++ - <random> 的线程安全使用

lua - 中间类问题

lua - Lua 的 Lint-ing 工具