tolua++ ignores assignment and inequality operator?
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=, ignori开发者_Go百科ng
- **tolua++ warning: No support for operator!=, ignoring
Is there any way around this?
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)
精彩评论