[英]what's the syntax of this c++ snippets?
经过多年的java,javascript,python,我不仅忘记了C ++,而且还混淆了这种语法。
http://heavycoder.com/tutorials/lua_embed.php
static const luaL_reg lualibs[] =
{
{ "base", luaopen_base },
{ NULL, NULL }
};
使用2D数组的lualibs init? luaL_reg是一个类型,但显然不是数组,
const luaL_reg *lib;
for (lib = lualibs; lib->func != NULL; lib++)
{
lib->func(l);
lua_settop(l, 0);
}
luaL_reg
是一个包含2个元素的struct
, 这是谷歌快速搜索的结果。
第一个片段是创建一个luaL_reg
struct
数组:
struct
初始化为两个值: { "base", luaopen_base }
luaL_reg
struct
设置为: { NULL, NULL }
底线,它不是 2D数组,而是structs
数组,其中每个struct
包含两个元素。
第二个例子现在应该是相当不言自明的; lib
是指向luaL_reg
struct
的指针。
luaL_reg
看起来可能如下所示。
typedef struct luaL_reg_t {
char const * const name;
void(*func)(< type_of_<l> >);
} luaL_reg;
可以使用{}
设置对象的成员,如下例所示,它将成员name
设置为指向"hello world"
的位置,并将func
为具有my_function
的地址。
luaL_reg obj = {"hello world", my_function};
在初始化数组成员时,也可以使用上一个代码段中显示的语法。 在下面的代码片段中,const luaL_reg实例数组被设置为包含两个对象,第一个具有name
= "base"
并且func
设置为luaopen_base
。
使事情清楚; 下面不是 2D数组,而是使用{}
初始化的const luaL_reg
数组来设置每个实例的成员。
static const luaL_reg lualibs[] =
{
{ "base", luaopen_base },
{ NULL, NULL }
};
最后一个元素用于简化迭代我们的数组,将两个成员设置为NULL
可以很容易地看到我们何时到达最后一个元素。
即将到来的片段中的循环利用了这一点。 只要成员func
不等于NULL
我们就没有到达数组的末尾。
for (lib = lualibs; lib->func != NULL; lib++) {
lib->func(l);
lua_settop(l, 0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.