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_()
, 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[] = {};
int r = fread( buf, , sizeof( buf ), f );
if( r > )
lua_string = std::string( buf, r );
fclose( f );
f = ;
} try
{
//2.加载Lua文件
int bRet = luaL_loadstring( L_, lua_string.c_str() );
if(bRet) {
OutputDebugStringA(lua_tostring( L_, - ) );
return false;
} //3.运行Lua文件 CHttpCall::~CHttpCall
bRet = lua_pcall( L_, , , );
if(bRet)
{
OutputDebugStringA(lua_tostring( L_, - ) );
return false;
}
} catch (...) {
OutputDebugStringA( lua_tostring( L_, - ) );
}
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();

在此记录一下。

LuaBridge 中C++类和继承示例的更多相关文章

  1. 关于Python中的类普通继承与super函数继承

    关于Python中的类普通继承与super函数继承 1.super只能用于新式类 2.多重继承super可以保公共父类仅被执行一次 一.首先看下普通继承的写法 二.再看看super继承的写法 参考链接 ...

  2. JavaScript中的类式继承和原型式继承

    最近在看<JavaScript设计模式>这本书,虽然内容比较晦涩,但是细品才发现此书内容的强大.刚看完第四章--继承,来做下笔记. 书中介绍了三种继承方式,类式继承.原型式继承和掺元类继承 ...

  3. java中阻止类的继承

    1.使用final来修饰类 final表示这个类是继承树的末端,不能被继承. 2.将类的构造方法声明为private的,再提供一个static的方法来返回一个类的对象. JAVA语言要求继承时必须在构 ...

  4. ES6中的类和继承

    class的写法及继承 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子     function Point(x, y) {  this.x = x;  this. ...

  5. [Android Studio] Android Studio中查看类的继承关系

    转载自:http://blog.csdn.net/hyr83960944/article/details/38098091 查看类的继承关系的快捷键F4,在Android Studio常用快捷键这篇文 ...

  6. Android Studio中查看类的继承关系

    查看类的继承关系的快捷键F4.在Android Studio经常使用快捷键这篇文章中.有写了.今天主要是讲一些关于这个快捷键出来的界面的一些配置.这块功能相对偏冷一些,可能非常多人都会用不到.可是关于 ...

  7. ES5中的类与继承

    最近在重新复习TypeScript,看到类这块的时候自然会和ES5中的类写法进行对比加深印象. 发现ES5的类与继承一些细节还是挺多的,时间久了容易忘记,特此记录下. 首先是ES5的类定义,这没什么好 ...

  8. python中的类与继承

    Class 类的定义以及实例的建立 Python中,类通过 class 关键字定义. 例如最简单的一个类定义可以为: class Person(object): pass Python 的编程习惯,类 ...

  9. ES6中。类与继承的方法,以及与ES5中的方法的对比

    // 在ES5中,通常使用构造函数方法去实现类与继承 // 创建父类 function Father(name, age){ this.name = name; this.age = age; } F ...

随机推荐

  1. 基于 自己定义注解 和 aop 实现使用memcache 对数据库的缓存 演示样例

    好久没更新blog了,在新公司打拼了两个月,每天都从早忙到晚,学到了非常多东西,可是没有时间来更新blog了.... 以下開始解说这次的主题 公司老大让我研究 ocs 就是阿里云的 开放缓存服务 点击 ...

  2. HDU Redraw Beautiful Drawings 推断最大流是否唯一解

    点击打开链接 Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 ...

  3. IOS开发-Swift新语言初见

    Safe Swift pairs increased type safety with type inference, restricts direct access to pointers, and ...

  4. Windows环境搭建与第一个C# Sample

    Redis入门 - Windows环境搭建与第一个C# Sample   什么是Redis? Redis是一个开源.支持网络.基于内存.键值对存储数据库,使用ANSI C编写.从2013年5月开始,R ...

  5. Java实现缓存(类似于Redis)

    Java实现缓存,类似于Redis的实现,可以缓存对象到内存中,提高访问效率.代码如下: import java.util.ArrayList; import java.util.HashMap; i ...

  6. POJ3233(矩阵二分再二分)

    题目非常有简单: Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + ...

  7. c# 播放器 支持所有格式

    原文:c# 播放器 支持所有格式 ---恢复内容开始--- 直接上代码 internal static class LibVlcAPI { internal struct PointerToArray ...

  8. ViewPaper实现轮播广告条

    使用V4包中的viewPaper组件自己定义轮播广告条效果. 实现viewpaper的滑动切换和定时自己主动切换效果. 上效果图 布局文件 <RelativeLayout xmlns:andro ...

  9. 【Linux 工作经常使用命令 】

    1, 批量杀某个程序 比方某个程序叫  url_info.py, 起了若干个进程 . 高速查杀. 先查看 ps aux | grep url_info.py 确认没问题 ,能够杀,则批量kill ps ...

  10. 第三方控件netadvantage UltraWebGrid如何生成多级跨行表头个人总结

    1.生成多级表头,横向和纵向跨度. 1>对于有字段的的表头合并:也就是(工期.项目经理信息除外)可以在前台通过spanx和spany属性控制.对于空字段(工资.项目经理必须通过后台动态添加),而 ...