lua-users home
lua-l archive

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

On Mon, Jan 31, 2011 at 15:27, Eric Clark <> wrote:
 > I am a pretty new user to developing with Lua and I am having some issues
> trying to set up an inheritance hierarchy with Lua. I have created the
> tables using some code that I found on the internet, but I do not really
> understand the code.
> Eric

it is really worth its money to buy "Programming in Lua, 2nd edition"
by Roberto Ierusalimschi. The outdated 1-st edition of the book
covering previous version Lua-5.0 is available online  for free.

> If so, could anyone give me some sample C/C++ code to show how you
> actually set the __index field to refer to a table?

After reading chapter on tables and metatables  many things will
become clear. E.g. you do not need C/C++ code at all to get/set a
table's  metatable.

--file: mt.lua
mt= {a=1, b=2}
mt.__index= mt
tbl= {}
setmetatable(tbl, mt)
print(tbl.a, tbl.b)

sh$ lua mt.lua
1        2