笔记C++for循环内变量的定义

 for (int i = 0; i < c; i++)
            {
                const CSTradeDetail_Data::tItem &item=*(data[i]);//数据结构的定义
                const QVariant cParas[] = { item.num, item.positionnum, item.m_OrderID, item.orderType,
                            item.merpCode, item.tradeTime, item.buyType, item.tradeNum, item.tradePrice,
                            item.holdPrice, item.oheNewLossP, item.oheNewProfitP, item.oheStopPrice,
                            item.remainNum, item.fee, item.openType, item.profit, item.profit,
                            item.banance, item.parttern, item.tradeType, item.udType, item.m_OrderUser,
                            item.m_OrderStrt, item.m_OrderID };//优化代码的QVariant定义
                if ((codes.isEmpty() || codes.contains(item.merpCode)) &&
                        ((openType == 0 || openType == 1) ? openType == item.openType : true))
                {
                    //		curHoldNum = ps.m_bType ? ps.m_dNumber : -ps.m_dNumber;
                    int itemp = 0;
                    lua_pushinteger(L, ++all);
                    lua_newtable(L);
                    for(int j=0;j<sizeof(cParas)/sizeof(cParas[0]);++j)
                    {
                        const QVariant &v=cParas[j];

                        if(v.type()==QVariant::String)
                        {
                            lua_pushinteger(L, ++itemp);
                            lua_pushstring(L, v.toString().toLocal8Bit().data());
                            lua_settable(L, -3);
                        }
                        else if(v.type()==QVariant::Double)
                        {
                            lua_pushinteger(L, ++itemp);
                            lua_pushnumber(L, v.toDouble());
                            lua_settable(L, -3);
                        }
                        else if(v.type()==QVariant::Int)
                        {
                                lua_pushinteger(L, ++itemp);
                                lua_pushnumber(L, v.toInt());
                                lua_settable(L, -3);
                        }
                    }

                    lua_getglobal(L, "MT");
                    lua_pushstring(L, "detailmeta");
                    lua_gettable(L, -2);
                    if (!lua_istable(L, -1))
                    {
                        Q_ASSERT(0);
                        lua_pop(L, 1);
                    }
                    else
                    {
                        lua_setmetatable(L, -3);
                    }
                    lua_pop(L, 1);
                    lua_settable(L, -3);

                }
            }
            Q_ASSERT(lua_gettop(L) == top + 1);


            return 1;
    }


}
在这段C++与Lua脚本的交互代码中,当for循环里面定义了变量的时候,起初我遇到的问题是为什么在for循环里面不会有重复定义的冲突,因为每次循环都会定义变量item,
cParas两个变量,后面经过仔细思考发现在for循环里面C++的处理机制应该是每次循环结束的时候会清空for循环里面的局部变量。这和函数的调用是一样的,首先将函数压栈
然后每次函数调用结束都会使得函数出站,里面的所有局部变量也就销毁了,for循环也是如此

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值