Hello I有以下似乎行之有效的代码,但我不敢肯定为什么——我先造了一个试验舱如下:
class testclass {
int ivalue;
public:
int getivalue();
void setivalue(int &v);
};
之后,注册测试舱(实际功能的借项,但基本面)。 它对表一米的登记没有进行。 (eti Value and seti Value are c function that打上同一名称的类别功能)
static const struct luaL_Reg arraylib_f [] = {
{"new", new_testclass},
{NULL, NULL}
};
static const struct luaL_Reg arraylib_m [] = {
{"set", setivalue},
{"get", getivalue},
{NULL, NULL}
};
int luaopen_testclass (lua_State *L) {
luaL_newmetatable(L, "LuaBook.testclass");
lua_pushvalue(L, -1); /* duplicates the metatable */
lua_setfield(L, -2, "__index");
luaL_register(L, NULL, arraylib_m);
luaL_register(L, "testclass", arraylib_f);
return 1;
}
The bit I don t understand is I m adding the functions to the __index for the metatable but when I run
a = testclass.new()
a:set(10)
print(a:get())
然后它按预期运作。 我不理解的是,为什么在我把这套装置装在元件中的时候,有人叫它呢? 难道我已经做些什么?
tia