Advertisement

LUA: Instantiating lua objects from c++ [SORTED] I think

Started by August 03, 2004 05:37 AM
2 comments, last by TonyFish 20 years, 3 months ago
How can I do this in c++? Let's say I have a c++ class (baseclass) which i extend in lua (derivedclass). I'd like to create an instace of derivedclass from the c++ code. I've looked through the docs but haven't had much success. Could someone please help em with this mater. Thanks Tony [Edited by - TonyFish on August 3, 2004 11:39:36 AM]
<Fish>{
You can instantiate it in a Lua VM (by executing code in the VM), but it isn't possible to create a C++ object for it (using the derived interface etc) if that's what you want to do.

Correct me if I'm wrong.

-psy
Advertisement
So I can't do something like:

a = create_new_lua_object("extended");
a.somefunc();
<Fish>{
I got an answer out of someone in the luabind mailinglist:

Instantiating a lua class is just a matter of calling the constructor like any other function. For example:

object my_class(get_globals(L)["my_class"]);

Since the object won't be stored anywhere in lua, you need to adopt the object. Otherwise it will be collected by lua.
i.e.

my_base* p = object_cast<my_base*>(my_class(), adopt(result));
<Fish>{

This topic is closed to new replies.

Advertisement