日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

LuaBridge 中C++类和继承示例

發布時間:2024/1/17 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LuaBridge 中C++类和继承示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

luabridge不是一個陌生的名字,GIT上已經有3-4年多沒有更新。將lua和C++相互調用封裝的很方便,比如以下示例代碼:

// // test code for luabridgeclass A { public:A(){}~A(){}public:std::string get_title() const{return title;}void set_title( const std::string& s ){title = s;}private:std::string title; };class B : public A { public:B() : A(){}~B(){} };void trace( const std::string& strOutput) {OutputDebugStringA(strOutput.c_str());OutputDebugStringA("\n"); }class lua_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_ );}bool run( ) {luabridge::setglobal<A*>( L_, (A*)&this->B_, "classb" );B_.set_title( "B.title ");std::string lua_string;FILE* f = fopen( "D:/test.lua", "r" );if( f ) {char buf[2048] = {0};int r = fread( buf, 1, sizeof( buf ), f );if( r > 0 ) lua_string = std::string( buf, r );fclose( f );f = 0;}try{//2.加載Lua文件 int bRet = luaL_loadstring( L_, lua_string.c_str() ); if(bRet) { OutputDebugStringA(lua_tostring( L_, -1 ) );return false;}//3.運行Lua文件 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_; };

// 運行代碼

? lua_test t;
? t.run();

?

lua_test 打開D:/test.lua文件并執行test_func方法,該方法創建了一個B的實例并打印實例的title屬性以及全局對象classb的title屬性

test.lua:

function test_func()local a = test.B();a.title = "abcdefg";trace( a.title )trace( classb.title ); endtest_func();

在此記錄一下。

?

轉載于:https://www.cnblogs.com/lovelylife/p/5436056.html

總結

以上是生活随笔為你收集整理的LuaBridge 中C++类和继承示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。