🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

class polymorphism in LUA

Started by
10 comments, last by nekoflux 20 years, 3 months ago
hey guys: I have several C++ classes which extend from a virtual class, and I want to be able to use the extended classes in LUA. How do I do this? Heres an example of what im trying to accomplish: My base class:

 class CMyBase{  
 public:
   CMybase();

   virtual int someFunc() = 0;
   int a;
};
 
My 2 classes extended from the base class:

class CBox : public CMyBase{  
 public:
   CBox();

   int someFunc(){
      return a - 32;
   }
};

class CSphere : public CMyBase{  
 public:
   CSphere();

   int someFunc(){
      return a + 14;
   }
};
 
now heres my lua pseidocode:

box = new CBox();
sph = new CSphere();

result_box = box:someFunc();
result_sphere = box:someFunc();
but for some reason this code crashes at runtime when I attempt to load the lua script. What am I doing wrong? I declare the module[] definition for all 3 classes, and it still doesnt seem to work..any ideas folks? thanks a lot!! neko [edited by - nekoflux on March 31, 2004 1:26:44 PM]
nekoflux
Advertisement
What binding generator are you using? "box = new CBox();" is unlikely to be valid Lua code. (And you know that Lua doesn''t need semicolons, right?)

"Sneftel is correct, if rather vulgar." --Flarelocke
Im using luabind

yes im aware that the lua code i inserted wont compile; its pseudocode.

I do the bindings using the module[] definitions

nekoflux
So what''s the actual code you''re using?

"Sneftel is correct, if rather vulgar." --Flarelocke
can you elaborate on what you mean by "what''s the actual code" I''m using? Do you mean like the actual lua code or the actual c++ code where I register the 3 c++ classes to the lua system.

as far as the lua code goes Im not doing anything out of the ordinary, just instantiating the 2 class instances and executing a method in each of them, which causes the crash.
Let me know what you want to see and Ill paste it in when I get home (5pm est)

thank you!

nekoflux
nekoflux
Both, really. Also, the error and the stack trace.

"Sneftel is correct, if rather vulgar." --Flarelocke
heres my c++ code that does the binding:

module(luaVM)  [  class_<CBox>("CBox")  .def(constructor<>())  .def("someFunc", &CBox::someFunc)  ];    



heres the lua code i attempt to run:

t = new CBox()p = t:someFunc()



The above code results in a silent crash (Im guessing this is windows segfault)

If I attempt to define the base class in lua I get a compile time error.

module(luaVM)  [  class_<CMyBase>("CMyBase")  .def(constructor<>())  ];    


heres the error:
\include\luabind\detail\constructor.hpp(99): error C2259: 'CMyBase' : cannot instantiate abstract class


so i leave the above code commented out because im not sure if I need to bind the base class to lua. maybe it has something to do with this?

neko

[edited by - nekoflux on March 31, 2004 5:47:26 PM]
nekoflux
Huh, interesting. It looks like luabind doesn''t play well with abstract classes (classes with pure virtual functions). You might try adding empty class bodies for all pure virtual functions (leaving them virtual, of course) and see if that helps matters any.

"Sneftel is correct, if rather vulgar." --Flarelocke
As for the silent crash, try running it in the debugger. If a crash doesn''t show up there, the code actually exited, rather than having an error.

"Sneftel is correct, if rather vulgar." --Flarelocke
quote: Original post by Sneftel
Huh, interesting. It looks like luabind doesn''t play well with abstract classes (classes with pure virtual functions). You might try adding empty class bodies for all pure virtual functions (leaving them virtual, of course) and see if that helps matters any.


luabind works fine with abstract classes. Registering a constructor for one won''t work though, for obvious reasons.

I think your "silent" crash is because the errors in your lua code. No such thing as ''new''..

sph = new CSphere()

should read

sph = CSphere();

This topic is closed to new replies.

Advertisement