Examples of C + + classes and inheritance in Luabridge

Source: Internet
Author: User
Tags lua

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.