初学者 LuaPlus 元表问题
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是标准的 Lua,看起来你正在使用一些我不熟悉的 C++ 包装器,但我可以做出一些猜测
__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
__index
is a special key when using metatables. If I have a tablet
and I try to index into it with a key offoo
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 getnil
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)