Possible flaw in handling of object references

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Possible flaw in handling of object references

Kristoffer Danielsson
Consider the C++ function below. The MyStruct and MyMember classes are both mapped into Lua.
 
struct MyMember
{
 int value;
};
 
struct MyStruct
{
 MyMember x;
};
 
void foo(MyStruct &m, const MyMember &x)
{
 m.x.value = 1;
 m.x.value += x;
}
 
Now we call the function from Lua:
local m = MyStruct()
local x = MyMember()
foo(m, x)
 
It seems correct at first, but the API is flawed. Parameter x might get altered if function is used as follows:
local m = MyStruct()
m.x = 3
foo(m, m.x)
 
Expected value of m.x: 4
Actual value of m.x: 2
 
What is the best practice to avoid this 'fake-const' problem?

I understand you can change the signature of foo to "void foo(MyStruct &m, MyMember x)", but that implies a possible performance hit, doesn't it? Is there a better way?
 
My feeling is that all bound functions should have non-reference parameters IF parameter type exists in 'this' AND is bound to lua. Correct? Is it possible that luabind could handle this automatically to avoid semantic errors?
 
Thanks


Ladda ner världens mest använda webbläsare nu. Ta med dig Hotmail i mobilen!
------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit.  See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user