Luabridge is not an unfamiliar name, and Git has more than 3-4 years of updates. It is convenient to call Lua and C + + to encapsulate each other, such as the following sample code:
////////////////////////////////////////////////////////////////////////////test code for LuabridgeclassA { Public: A () {}~A () {} Public: std::stringGet_title ()Const { returntitle; } voidSet_title (ConstSTD::string&s) {title=s; }Private: std::stringtitle;};classD | PublicA { Public: B (): A () {}~B () {}};voidTraceConstSTD::string&stroutput) {Outputdebugstringa (Stroutput.c_str ()); Outputdebugstringa ("\ n");}classlua_test{ Public: Lua_test (): L_ (0), B_ () {L_=lual_newstate (); Lual_openlibs (L_); Luaopen_string (L_); Luabridge::getglobalnamespace (L_). AddFunction ("Trace", Trace). Beginnamespace ("Test"). Beginclass< A > ("A"). Addconstructor<void(*) (void) >(). AddProperty ("title", &a::get_title, &a::set_title). Endclass (). Deriveclass< B, A > ("B"). Addconstructor<void(*) (void) >(). Endclass (). Endnamespace (); } ~lua_test () {lua_close (l_); } BOOLrun () {Luabridge::setglobal<A*> (L_, (A *) & This->b_,"CLASSB" ); B_.set_title ("B.title"); STD::stringlua_string; FILE* F = fopen ("D:/test.lua","R" ); if(f) {Charbuf[2048] = {0}; intR = fread (buf,1,sizeof(BUF), f); if(R >0) lua_string= std::string(buf, R); Fclose (f); F=0; } Try { //2. Loading LUA files intBRet =lual_loadstring (L_, Lua_string.c_str ()); if(BRet) {Outputdebugstringa (lua_tostring (L_,-1 ) ); return false; } //3. Run the Lua file Chttpcall::~chttpcallBRet = Lua_pcall (L_,0,0,0); if(BRet) {Outputdebugstringa (lua_tostring (L_,-1 ) ); return false; } } Catch (...) {Outputdebugstringa (lua_tostring (L_,-1 ) ); } return true; }Private: Lua_state*L_; B b_;};
Run Code
Lua_test T;
T.run ();
Lua_test Open the D:/test.lua file and execute the Test_func method, which creates an instance of B and prints the title property of the instance and the title property of the Global object ClassB
Test.lua:
function Test_func () local a = test. B (); " ABCDEFG " ; Trace (a.title) Trace (classb.title); End Test_func ();
Record it here.
Examples of C + + classes and inheritance in Luabridge