zoukankan      html  css  js  c++  java
  • lua调用c++函数返回值作用

      2015/05/28

      lua调用c++接口,返回给lua函数的是压入栈的内容,可以有多个返回值。但是c++接口本身也是有返回值的,这个返回值也非常的重要,会决定最后返回到lua函数的值的个数。

    (1)c++自定义类

    int Test::getMsg(lua_State* L){
        lua_pushnumber(L, 100);
        lua_pushnumber(L, 200);
        return 2;
    }

    (2)tolua++导出的lua调用的c++接口(部分有修改)

    int lua_cocos2dx_custom_Test_getMsg(lua_State* tolua_S)
    {
        int argc = 0;
        Test* cobj = nullptr;
        bool ok  = true;
    
        cobj = (PlaceByPoint*)tolua_tousertype(tolua_S,1,0);
    
        {
            int ret = cobj->getMsg(tolua_S);
            tolua_pushnumber(tolua_S,(lua_Number)ret);
            return 1;
        }
    
        return 0;
    }

    (3)使用

    local p = cc.Test:create()
    local a, b, c = p:getMsg()
    print(a, b, c)

    结果是:2, nil, nil

      按照一般的理解,结果应该是100, 200, 2。所以去查了下资料,发现自己好久没用忘记了,c++接口的返回值也非常的重要。“当需要向 Lua 返回值的时候,C 函数只需要把它们以正序压到堆栈上(第一个返回值最先压入),然后返回这些返回值的个数。在这些返回值之下的,堆栈上的东西都会被 Lua 丢掉。”[1]

    参考:

    [1]

  • 相关阅读:
    文件操作
    需特别注意的地方(关于内存机制)
    数据类型的汇总和功能
    python之http请求及响应
    8.centos7进入单用户
    Android Studio使用总结
    django之数据库models
    django之错题集
    python之mysql安装配置
    python之pycharm的debug调试使用
  • 原文地址:https://www.cnblogs.com/pk-run/p/4536914.html
Copyright © 2011-2022 走看看