此 C++ 代码段的语法是什么?
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 }
};
lualibs 初始化 2D 数组? 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
。
为了清楚起见;下面的不是 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);
}
luaL_reg
是一个包含 2 个元素的struct
,这就是快速谷歌搜索的结果。
第一个代码片段是创建一个luaL_reg
struct
数组:
- 第一个
struct
使用以下两个值进行初始化:{ "base", luaopen_base }
- 数组中的第二个
luaL_reg
struct
设置为:{ NULL, NULL }
最重要的是,它不是一个 2D 数组,而是一个 structs
数组,其中每个struct
包含两个元素。
第二个例子现在应该是相当不言自明的; lib
是指向luaL_reg
struct
的指针。
相关文章:
- C++避免重复声明的语法是什么
- 使用基类指针调用基类的值构造函数的语法是什么?
- 这行/语法是什么意思?
- 初始化对象以在 C++08 中作为参数传递的首选语法是什么?
- 将引用绑定到指针的语法是什么?(各种)
- 这个typedef和转换运算符语法是什么意思
- 此语法是什么意思.(::*).
- 将显式指定的函数模板重载作为模板参数传递的正确语法是什么?
- 使用 pybind11 绑定 typedef 类型的正确语法是什么?
- 将此类传递给引用的正确语法是什么?
- 带有约束的可变参数模板的'requires'表达式的语法是什么?
- 在TensorFlow C++中用于feed_dict的语法是什么?
- 调用模板化类内的枚举类枚举器的正确语法是什么
- 返回没有 typedef 的成员函数指针的语法是什么?
- 将强制转换运算符重载到 std::map 的正确语法是什么
- 这种增量语法是什么意思
- 返回结构的方法的正确语法是什么
- C 中的(*)语法是什么意思
- 堆栈(int = 10),这个语法是什么意思(C++)
- variadic模板阵列参考参数的语法是什么?