用了多年java、javascript、python,不仅忘记了C++,连语法都搞混了。
http://heavycoder.com/tutorials/lua_embed.php
static const luaL_reg lualibs[] =
{
{ "base", luaopen_base },
{ NULL, NULL }
};
lualibs 初始化二维数组? luaL_reg是一个类型,但显然不是数组,
const luaL_reg *lib;
for (lib = lualibs; lib->func != NULL; lib++)
{
lib->func(l);
lua_settop(l, 0);
}
最佳答案
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
。
把事情说清楚;下面不是二维数组,而是一个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);
}
关于c++ - 这个 C++ 片段的语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8542653/