lua-users home
lua-l archive

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



Scott DC wrote:
I've been working with a few of the wiki examples using Lua5 and lunar.h -- http://lua-users.org/wiki/CallingLuaFromCpp What I want to know is the best way to access super/parent class methods of a C++ object.

I used the 'Account' example and gave it a parent class called 'Super' with methods 'incNum' and 'decNum'.


Hi Scott,

Good question, but I haven't got a good answer yet.

It would be nice if one could do something like Philipp Janda just suggested http://article.gmane.org/gmane.comp.lang.lua.general/4987

For example:

  getmetatable(Account).__index = Super
  a = Account(100)
  a:incNum()

The problem is that the Lunar<Super>::thunk function calls Lunar<Super>::check which calls luaL_checkudata to make sure the 'self' userdata has a Super metatable. This is why the above example gives an error like:

  calling `incNum' on bad self (Super expected, got userdata)

One solution would be to add some parent class info to the RegType structure and change the thunk/check functions. Another would be a hash table of parent classes stored in an upvalue. I'll try some stuff tomorrow.

I wonder how the luabind and LuaPlus guys handle inheritance.

Note that C++ and OO are not my forte, so using RTTI (or changing the luaL_checkudata to use fancy templates so that it knows about the Super/Account relationship) is out of my league.

- Peter