你好,我有這似乎工作,但我不知道爲什麼下面的代碼位 - 我已經建立了一個識別TestClass如下盧阿元表登記
class testclass {
int ivalue;
public:
int getivalue();
void setivalue(int &v);
};
,然後註冊的識別TestClass (實際功能中遺漏了一些位,但它們非常基本)。這是我沒有遵循的metatables的註冊。 (etivalue和setivalue是叫同一個名字的類函數的C函數)
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;
}
我不明白的是我加入的功能到__index的元表,但 當我運行
位a = testclass.new()
a:set(10)
print(a:get())
然後它按預期工作。我不明白的是爲什麼當我認爲我已經將它加載到__index元表中時,該集被調用?那是我所做的還是別的什麼?
TIA