lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>>>
 In that situation I add a :clone() method (or have a constructor which
can take another object and copy it), written in C.  That makes the
copying behaviour explicit in the Lua code.  You don't want assignment
to suddenly start behaving differently, even if it were possible.

<<<

True, overriding assignment isn't a very straightforward thing to do.  Let me express my underlying problem of why I want to do so, and maybe you could suggest a solution.

I have my classes, A and B, as described above, where A has a member b of type B.

I then call a lua function from c++ with an object of type A as a parameter, like this:

c++:

A* a = new A();
LuaObject::globals(L)["myFunc"](a);

lua:

function myFunc(a)
    b = a.b
end

Now, when I call myFunc, I want ownership of the newly allocated A to transfer to lua.  tolua++ has a function to do this, called tolua_pushusertype_and_takeownership, so I'm good there.   I _also_ want the b member to be stored in a global variable, so I can use it after myFunc exits.  The problem is, a's ownership gets transferred to lua, and when it's cleaned up, the b member is destroyed also.  This means the global b is now pointing to an invalid B object.  

So, if I could have this line:
b = a.b

... do a copy instead of a reference assignment, I'd be ok.  There also might be some other ways to solve this problem, so I'm not hung up on using this method.




















On Tue, Sep 23, 2008 at 1:44 AM, Geoff Richards <Ni1phahs@ungwe.org> wrote:
On Mon, Sep 22, 2008 at 02:18:15PM -0700, Matthew Armstrong wrote:
> Let me add some more information.  Classes A and B are bound with the tolua++
> binding library, and are dealt with as userdata.

In that situation I add a :clone() method (or have a constructor which
can take another object and copy it), written in C.  That makes the
copying behaviour explicit in the Lua code.  You don't want assignment
to suddenly start behaving differently, even if it were possible.

--

--- Geoff Richards -------------><-------------- http://ungwe.org/ ---
"I tried to fling my shadow at the moon,
 The while my blood leapt with a wordless song."  --  Theodore Roethke