#include <iostream>
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>
}
using namespace std;
class Human
{
private:
int age;
char name[10];
double salary;
double height;
bool isMale;
public:
void init(lua_State *ls)
{
lua_settop(ls,0);
lua_getglobal(ls,"age");
lua_getglobal(ls,"name");
lua_getglobal(ls,"salary");
lua_getglobal(ls,"height");
lua_getglobal(ls,"isMale");
if (lua_isnumber(ls,1) == 1)
{
age=(int)lua_tonumber(ls,1);
}
if (lua_isstring(ls,2) == 1)
{
strcpy(name,lua_tostring(ls,2));
}
if (lua_isnumber(ls,3) == 1)
{
salary=(double)lua_tonumber(ls,3);
}
if (lua_isnumber(ls,4) == 1)
{
height=(double)lua_tonumber(ls,4);
}
if (lua_isboolean(ls,5) == 1)
{
isMale=(bool)lua_toboolean(ls,5);
}
}
void printInfo()
{
cout<<age<<endl;
cout<<name<<endl;
cout<<salary<<endl;
cout<<height<<endl;
cout<<isMale<<endl;
}
};
int main()
{
Human hm;
lua_State * L;
L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L,"test.lua");
hm.init(L);
hm.printInfo();
lua_close(L);
getchar();
return 0;
}
5.编译运行