2

Lua を C++ に統合しています。現在、「クラス」として動作するこのテーブルがあり、一部の関数では、実際にはテーブルである「自己」引数が必要です。Lua コード:

a = {
numb = 5,

create = function(a)
    print(a);
end,

increment = function(self)
                            --self.numb = 6;
                            print(self.numb);
end,

decrement = function(self,i)
                            self.numb = self.numb-i;
                            print(self.numb);
end
};
b = a;

そして、関数を呼び出すための C++ ビット (Lua を C++ で実行しています)

luaL_openlibs(L);

luaL_dofile (L,"main.lua");

lua_getglobal(L, "a");
lua_getfield(L, -1, "increment");

string arg = "a";

lua_pushliteral(L,"a");

lua_pcall(L ,1,0,0);

printf(" \nI am done with Lua in C++.\n");

lua_close(L);

では、自己引数をテーブルとして関数 increment に渡すにはどうすればよいでしょうか?

どんな助けでも大歓迎です

4

1 に答える 1

2

Lua 5.1lua_getglobalでは、ええと、テーブルのようにグローバルaを取得していました。それを使用して、数行上のテーブルを取得しています。あなたがする必要があるのは、その値を複製して関数に渡すことだけです

 lua_getglobal(L, "a"); // the table a is now on the stack
 lua_getfield(L, -1, "increment"); // followed by the value of a.increment

 lua_pushvalue(L,-2); // get the table a as the argument

 lua_pcall(L,1,0,0);
于 2012-06-27T20:27:50.737 に答える