[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Inheritance
- From: Florian Weimer <fw@...>
- Date: Sun, 09 May 2010 21:53:22 +0200
* Christoph Schreiber:
> Here is the full function. I don't understand how lua knows which
> base class to call. I return only the method name. I always thougt
> the return value (1 in this case) tells lua how many values I return
> on the stack.
>
> int ChildClass::index(lua_State* L)
> {
> // searches for the method in child's metatable
> const char* key = luaL_checkstring(L, 2);
> lua_getmetatable(L, 1);
> lua_getfield(L, -1, key);
>
> // if it doesn't find the method it calls
> // the method of the base class
> if(lua_isnil(L, -1))
> {
> luaL_getmetatable(L, BaseClass::classId);
> lua_getfield(L, -1, key);
> }
> return 1;
> }
I think it just gets the base class's metatable from the registry,
using the string BaseClass::classId. Note that lua_getmetatable and
luaL_getmetatable are different functions.