本文是最后一篇C/C++与Lua交互的教程,在此之后,我们会结合Cocos2D-X来介绍Lua绑定。本文主要介绍如何绑定一个简单的C++类到Lua里面,并且提供Lua的面向对象访问方式。
[b]绑定C++类[/b]
[b]定义C++类[/b]
首先,我们定义一个Student类,它拥有名字(字符串类型)和年龄(整型),并且提供一些getter和setter,最后还提供了一个print方法.这里有Student类的定义和实现:[url=http://git.oschina.net/zilongshanren/Lua-Tutorials/blob/master/LuaCocos2D-X/Classes/Student.h]Student.h[/url]和[url=http://git.oschina.net/zilongshanren/Lua-Tutorials/blob/master/LuaCocos2D-X/Classes/Student.cpp]Student.cpp[/url]
[b]编写绑定代码[/b]
首先,让我们编写在Lua里面创建Student对象的方法:
[url=http://git.oschina.net/zilongshanren/Lua-Tutorials]本文源代码[/url],注意在LuaCocos2D-X工程里面。