LuaBridge 中C++类和继承示例

luabridge不是一个陌生的名字,GIT上已经有3-4年多没有更新。将lua和C++相互调用封装的很方便,比如以下示例代码:

//
// test code for luabridge

class 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::~CHttpCall
      bRet = 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 );
end

test_func();

在此记录一下。

 

转载于:https://www.cnblogs.com/lovelylife/p/5436056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++继承是一种面向对象编程的重要概念,它允许一个(称为子或派生继承另一个(称为父或基)的属性和行为。子可以使用父的成员变量和成员函数,并且可以添加自己的新成员变量和成员函数。 下面是继承的定义和示例代码: 定义: 1. 继承使用关键字`class`后跟着冒号和父的名称进行定义。 2. 继承有三种型:公有继承(public inheritance)、私有继承(private inheritance)和保护继承(protected inheritance)。 3. 公有继承表示父的公有成员在子仍然是公有的,私有成员在子无法访问。 4. 私有继承表示父的公有成员在子变为私有的,私有成员在子无法访问。 5. 保护继承表示父的公有成员在子变为保护的,私有成员在子无法访问。 示例代码: ```cpp // 父 class Shape { protected: int width; int height; public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } }; // 子 class Rectangle : public Shape { public: int getArea() { return width * height; } }; int main() { Rectangle rect; rect.setWidth(5); rect.setHeight(10); int area = rect.getArea(); std::cout << "Area: " << area << std::endl; return 0; } ``` 在上述示例,我们定义了一个父`Shape`和一个子`Rectangle`。子`Rectangle`通过`public`关键字进行公有继承,表示子可以访问父的公有成员。 在`Rectangle`子,我们可以使用父`Shape`的成员变量`width`和`height`,并且可以使用父的成员函数`setWidth()`和`setHeight()`来设置这些成员变量的值。子还添加了一个自己的成员函数`getArea()`,用于计算矩形的面积。 在`main()`函数,我们创建了一个子对象`rect`,并使用父和子的成员函数来设置宽度和高度,并计算矩形的面积。 继承提供了代码重用和扩展的能力,使得我们可以构建层次化的结构,并在子添加新的功能。通过继承,子可以继承的特性,并且可以进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值