lua-users home
lua-l archive

Re: metatable and c++ assistance/thoughts

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


Hello Robert,
Tuesday, October 19, 2004, 10:53:11 AM, you wrote:
RS> I'm trying to work with objects in both lua and c++, and am playing with
RS> creating a table from c++ with static methods from c++ linked into table
RS> functions, and a pointer to the c++ object stored in the table with some
RS> appropriate name (this, for instance).
RS> Then, when a table method is called with the : operator, the table is
RS> passed, I can extract the pointer, and everything is groovy then to call
RS> the object from c++.
You can try another way to do this, table is not needed.
Create a userdata to wrap c++ object is enough. If you want to simulate c++ member function
in lua, you can simply use the upvalue to pass 'this'.
set '__index' into userdata's metatable, and then, when you need call c++ object's member function in lua,
call it with '.' operator instead of ':' .
userdata's '__index' method put the c++ object pointer into upvalue, and the member function can use it later.
If you need another lua table to store some related data, you can create a table in lua's registry,
and store the reference number in c++ object.
-- 
Best regards,
 cloudwu mailto:cloudwu@163.com
 http://www.codingnow.com
[失去灯光之后, 我们拥有了满天的星曜]

AltStyle によって変換されたページ (->オリジナル) /