托卢阿++忽略赋值和不等式运算符?

发布于 2024-10-09 01:57:50 字数 189 浏览 4 评论 0原文

我正在将 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 技术交流群。

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

发布评论

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

评论(1

噩梦成真你也成魔 2024-10-16 01:57:50

我不熟悉 tolua++,但它不支持其中任何一个也是有道理的。 tolua++ 只是礼貌地通知您,这样您就不会认为代码有任何作用。

赋值运算符在 Lua 上下文中没有任何意义,并且 ~= 运算符是 == 的否定,因此实现 operator== 可以同时处理 ==~= 为您的 Lua 对象。

编辑:使用这个空间来回答下面提出的问题,以便我可以包含代码:

确实,这解释了不等式关系,但是赋值呢?

在 Lua 中,变量不是类型化的,它们只是值的名称。赋值运算符将任何类型的新值与该名称关联,它不会修改与该名称关联的先前值(例如,该值存在于内存中的某个位置,未更改,如果没有进一步引用它,则等待被垃圾收集)存在)。想想赋值对于全局变量意味着什么:

print(math) --> table: 00516620
math = "foo"
print(math) --> foo

第二行相当于:

_G.math = "foo"

换句话说,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 implementing operator== takes care of both == and ~= for your Lua object.

EDIT: Using this space to answer a question posed below so I can include code:

True, that explains the inequality relation, but what about assignment?

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:

print(math) --> table: 00516620
math = "foo"
print(math) --> foo

That second line is equivalent to:

_G.math = "foo"

In other words, math=val is replacing the value at _G["math"], so would it mean to override operator= 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 our math="foo" example, the __newindex would be on _G not math, 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)

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