如何创建一个新函数并通过另一个函数传递它

基本上我不知道如何做到这样:

    lua_pushcfunction(L, int func(lua_State* L) {
      printf("hello");
      return 0;
    });

我尝试了很多东西,但它们都不起作用

点赞
用户106104
用户106104

两种方法:

  1. 定义函数,然后将其推送。

    int func(lua_State* L) {
      printf("hello");
      return 0;
    };
    
    // 后来...
    lua_pushcfunction(L, func);
    

    这是在 C 或 C++11 以前唯一的方法。

  2. 使用 Lambda 表达式(也称为匿名函数):

    lua_pushcfunction(L, [](lua_State* L) -> int {
      printf("hello");
      return 0;
    });
    
2019-03-07 23:42:14