托卢阿++忽略赋值和不等式运算符?
我正在将 Lua 嵌入到 C++ 类中。
看来 tolua++ 忽略了我的类中的一些重载运算符。
tolua++ 发出以下警告:
- **tolua++ 警告:不支持运算符=,忽略
- **tolua++ 警告:不支持运算符!=,忽略
有没有办法解决这个问题?
I am embedding Lua in a C++ class.
It seems that tolua++ is ignoring some overloaded operators of my class.
tolua++ issues the following warnings:
- **tolua++ warning: No support for operator=, ignoring
- **tolua++ warning: No support for operator!=, ignoring
Is there any way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 tolua++,但它不支持其中任何一个也是有道理的。 tolua++ 只是礼貌地通知您,这样您就不会认为代码有任何作用。
赋值运算符在 Lua 上下文中没有任何意义,并且
~=
运算符是==
的否定,因此实现operator==
可以同时处理==
和~=
为您的 Lua 对象。编辑:使用这个空间来回答下面提出的问题,以便我可以包含代码:
在 Lua 中,变量不是类型化的,它们只是值的名称。赋值运算符将任何类型的新值与该名称关联,它不会修改与该名称关联的先前值(例如,该值存在于内存中的某个位置,未更改,如果没有进一步引用它,则等待被垃圾收集)存在)。想想赋值对于全局变量意味着什么:
第二行相当于:
换句话说,
math=val
正在替换_G["math"]
处的值,那么这是否意味着重写数学对象的operator=
?没有什么。最接近修改赋值运算符的是 __newindex 元方法,它仅适用于表/用户数据,因此对局部变量没有影响。在我们的
math="foo"
示例中,__newindex
将位于_G
而不是math
,并且在这种情况下甚至不会被调用,因为_G.math
具有现有值(当您尝试将值分配给不存在的键时,会调用__newindex
)I'm not familiar with tolua++, but it makes sense that it wouldn't support either of those. tolua++ is just politely informing you so you don't think that code is having any effect.
The assignment operator makes no sense in the context of Lua, and the
~=
operator is the negation of==
, so implementingoperator==
takes care of both==
and~=
for your Lua object.EDIT: Using this space to answer a question posed below so I can include code:
In Lua, variables aren't typed, they are just names for values. The assignment operator associates a new value, of any type, with that name, it doesn't modify the previous value associated with the name (e.g. that value exists somewhere in memory, unchanged, waiting to be garbage collected if no further references to it exist). Think about what assignment means for a global variable:
That second line is equivalent to:
In other words,
math=val
is replacing the value at_G["math"]
, so would it mean to overrideoperator=
for the math object? Nothing.The closest you can get to modifying the assignment operator is the
__newindex
metamethod, which only applies to tables/userdata, so would have no effect on locals. In the case of ourmath="foo"
example, the__newindex
would be on_G
notmath
, and wouldn't even be invoked in this case because_G.math
has an existing value (__newindex
is invoked when you try to assign a value to a non-existent key)