lua-users home
lua-l archive

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

On 2/24/2013 6:20 AM, TheGreat Guru wrote:
> Hello all :) Just wondering if the is a way to bind c++ classes to
> lua BUT with c++ inheritance support ! I mean if the is a way to
> write lua scripts and create objects of a c++ subClass that can
> access the methods of baseClass. What i am looking for is some link
> for a good tutorial OR some custom code sample/s !
> *IMPORTANT : I dont want to use a library like luaBind , toLua ,
> LuaCpp or even SWIG . Just simple lua C API .

I would recommend digging into the giant pile of code samples that is LuaBind, which is the only existing library I'm sure does what you want. Since it does what you want, it therefore is a working sample of the kind of code you need to write. LuaBind, underneath it all, just uses the Lua C API, after all.

The short answer is that you need to build, in C++, a derived instance of each class you want to extend. Each derived instance would implement virtual functions for each of the member functions you want to override (and would probably hold a pointer to the lua_State). Each of those functions would have a corresponding member variable that could point at a Lua function to call to implement custom behavior (and presumably do an "if NULL then call the base class" if you decided not to override it in Lua). Then you'd create a constructor for each "derived class" in Lua that creates an instance of that generic Lua binding class, and the constructor would fill in the function pointers, which could just be function NAMES to call, or could be indices to Lua function objects that you've stashed in the registry, or whatever.

If the above paragraph doesn't make sense to you, then I'd STRONGLY recommend just USING LuaBind (yes I know you SAID you didn't want to use it, but, well, you've been warned). What you're trying to do is a pretty advanced technique, and I wouldn't recommend it to someone without much Lua extension experience. Why DON'T you want to use LuaBind, by the way?