我是靠谱客的博主 无辜糖豆,这篇文章主要介绍笔记C++for循环内变量的定义,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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; } }
复制代码
1
2
在这段C++与Lua脚本的交互代码中,当for循环里面定义了变量的时候,起初我遇到的问题是为什么在for循环里面不会有重复定义的冲突,因为每次循环都会定义变量item,
复制代码
cParas两个变量,后面经过仔细思考发现在for循环里面C++的处理机制应该是每次循环结束的时候会清空for循环里面的局部变量。这和函数的调用是一样的,首先将函数压栈
复制代码
然后每次函数调用结束都会使得函数出站,里面的所有局部变量也就销毁了,for循环也是如此
复制代码

最后

以上就是无辜糖豆最近收集整理的关于笔记C++for循环内变量的定义的全部内容,更多相关笔记C++for循环内变量内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部