Skip to main content
gamedev.net
🔒 Locked

Deriving lua classes from c++

Started by CableGuy 0 replies 800+ views
Original Post
CableGuy
CableGuy
Hi, I'm new to Lua so bear with me. I followed luabinds manual regarding deriving lua classes from ones defined in c++. But It doesn't work: C++:
class Test
{
public:
	Test(){cout << "Test constructor.\n";}
	virtual void Over(){};	
	void DoIt() 
	{
		cout << "DoIt!" << endl;		
	}
};
class TestWrapper : public Test,public luabind::wrap_base
{
public:
	TestWrapper() : Test()
	{

	}
	virtual void Over()
	{
		call<void>("Over");
	}
	static void DefaultOver(Test *ptr)
	{
		ptr->Test::Over();
	}
};
luabind::module(L)
[
	luabind::class_<Test,TestWrapper>("Test")
	.def(luabind::constructor<>())
	.def("DoIt",&Test::DoIt)
	.def("Over",&Test::Over,&TestWrapper::DefaultOver)
];
Lua:
class 'MyTest' (Test)
function MyTest:__init() super() end
t=MyTest()
t:DoIt()
t:Over()
The problem is if I put the super in the class constructor the program just exits and if I don't put it there the vtable is not constructed correctly(The constructor of Test is never called). To me it looks exactly like the example given in the manual... Thanks in advance.
CableGuy
CableGuy
I solved it but I don't know why. In the constructor instead of calling super() I did Test.__init(self) and it worked. I have no idea why using super didn't work though.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.