初学者 LuaPlus 元表问题

发布于 2024-12-02 04:18:32 字数 771 浏览 0 评论 0原文

我是 Lua/LuaPlus 的新手,并试图弄清楚元表是如何工作的。

这段代码取自手册:

LuaObject metaTableObj = state->GetGlobals().CreateTable("MultiObjectMetaTable");
metaTableObj.SetObject("__index", metaTableObj);
metaTableObj.RegisterObjectFunctor("Print", &MultiObject::Print);

在第一行中我们创建了一个新表,但第二行有点令人困惑。在我们刚刚创建的表中,我们将键 __index 的元素设置为等于表本身。为什么选择 __index 作为键以及为什么将表的元素设置为等于表本身?

然后在下一段代码中:

MultiObject obj1(10);
LuaObject obj1Obj = state->BoxPointer(&obj1);
obj1Obj.SetMetaTable(metaTableObj);
state->GetGlobals().SetObject("obj1", obj1Obj);

我们创建一个 C++ 对象,通过 BoxPointer 调用将其地址与 LuaObject 关联起来,并设置元表以便我们可以使用 Print 函数。

但对于最后一行,只是创建一个名为“obj1”的全局 Lua 变量吗?此时“obj1”和“MultiObjectMetaTable”将是全局Lua变量吗?

I'm brand new to Lua/LuaPlus and trying to figure out how metatables work.

In this code taken from the manual:

LuaObject metaTableObj = state->GetGlobals().CreateTable("MultiObjectMetaTable");
metaTableObj.SetObject("__index", metaTableObj);
metaTableObj.RegisterObjectFunctor("Print", &MultiObject::Print);

In the first line we create a new table, but the second line is a little confusing. In this table we just created, we are setting the element with key __index equal to the table itself. Why is __index chosen as a key and why set an element of the table to be equal to the table itself?

And then in the next section of code:

MultiObject obj1(10);
LuaObject obj1Obj = state->BoxPointer(&obj1);
obj1Obj.SetMetaTable(metaTableObj);
state->GetGlobals().SetObject("obj1", obj1Obj);

We create a C++ object, associate its address with a LuaObject via the BoxPointer call, and set the metatable so that we can use the Print function.

But for the last line, is that just creating a global Lua variable called "obj1"? At this point "obj1" and "MultiObjectMetaTable" will be global Lua variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2024-12-09 04:18:32

这不是标准的 Lua,看起来你正在使用一些我不熟悉的 C++ 包装器,但我可以做出一些猜测

在第一行中我们创建了一个新表,但第二行是
有点令人困惑。在我们刚刚创建的表中,我们设置
键 __index 等于表本身的元素。为什么是__index
选择作为键以及为什么将表的元素设置为等于
表本身?

__index 是使用元表时的特殊键。例如,如果我有一个表t,并且我尝试使用foo 键对其进行索引,那么我自然会取回与该键关联的值。但假设那里什么也没有。通常,如果您尝试索引到没有任何内容的位置,您将得到 nil 返回。

但如果您有一个元表,其中包含特殊键 __index ,则情况并非如此!如果您有一个带有 __index 函数或表的元表,它将使用它来查找您的值。如果您有一个表分配给 __index ,就像您在此处所做的那样,它将查看该表并返回您提供的键处的值。这允许您获得类似继承的行为。即,如果表 t 没有该值,则默认为另一个表中的值。

但是对于最后一行,只是创建一个名为“obj1”的全局 Lua 变量吗?此时“obj1”和“MultiObjectMetaTable”将是全局Lua变量?

正如我提到的,这不是标准Lua,所以我不完全确定那里发生了什么。 (不过,混合使用 C++ 和 Lua 可能会很棘手,因此当您仍在学习 Lua 时,最好坚持使用 C 接口,这样您就可以了解真正发生的情况。一旦您了解了,您就可以转向更自动化的解决方案)

This is not standard Lua, it looks like you're using some C++ wrapper that I'm unfamiliar with, but I can make some guesses

In the first line we create a new table, but the second line is a
little confusing. In this table we just created, we are setting the
element with key __index equal to the table itself. Why is __index
chosen as a key and why set an element of the table to be equal to the
table itself?

__index is a special key when using metatables. If I have a table t and I try to index into it with a key of foo for example, naturally, I'll get back the value associated with that key. But lets say there's nothing there. Normally if you try to index into a spot that has nothing, you'll get nil back.

But not if you have a metatable with the special key __index in it! If you have a metatable with an __index function or table, it'll use that to find you your value. If you have a table assigned to __index as you do here, it'll look into that table and return the value at the key you provided. This allows you get inheritance-like behavior. i.e. if table t doesn't have this value, default to the value in this other table instead.

But for the last line, is that just creating a global Lua variable called "obj1"? At this point "obj1" and "MultiObjectMetaTable" will be global Lua variables?

As I mentioned, this is not standard Lua, so I'm not totally sure what's happening there. (Mixing C++ and Lua can get tricky though, so while you're still learning Lua it's probably better that you stick to the C interface so you can understand whats really happening. Once you understand that you can move on to more automated solutions)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文