简体   繁体   English

这个c ++片段的语法是什么?

[英]what's the syntax of this c++ snippets?

After years of java, javascript, python, I am not only forget C++, but also confuse that syntax. 经过多年的java,javascript,python,我不仅忘记了C ++,而且还混淆了这种语法。

http://heavycoder.com/tutorials/lua_embed.php http://heavycoder.com/tutorials/lua_embed.php

static const luaL_reg lualibs[] =
{
        { "base",       luaopen_base },
        { NULL,         NULL }
};

lualibs init with a 2D array? 使用2D数组的lualibs init? luaL_reg is a type, but obviously is not a array, luaL_reg是一个类型,但显然不是数组,

const luaL_reg *lib;

for (lib = lualibs; lib->func != NULL; lib++)
{
    lib->func(l);
    lua_settop(l, 0);
}

luaL_reg is a struct with 2 elements, this is what a quick Google search turned up about it. luaL_reg是一个包含2个元素的struct是谷歌快速搜索的结果。

The first snippet is creating an array of luaL_reg struct s: 第一个片段是创建一个luaL_reg struct数组:

  • The first struct is initialized with the two values: { "base", luaopen_base } 第一个struct初始化为两个值: { "base", luaopen_base }
  • The second luaL_reg struct in the array is set to: { NULL, NULL } 数组中的第二个luaL_reg struct设置为: { NULL, NULL }

Bottom line, it's not a 2D array, but an array of structs , where each struct contains two elements. 底线,它不是 2D数组,而是structs数组,其中每个struct包含两个元素。

The second example should now be fairly self-explanatory; 第二个例子现在应该是相当不言自明的; lib is a pointer to a luaL_reg struct . lib是指向luaL_reg struct的指针。

luaL_reg probably looks something as the below. luaL_reg看起来可能如下所示。

typedef struct luaL_reg_t {
  char const * const name;
  void(*func)(< type_of_<l> >);
} luaL_reg;

The members of an object can be set using {} as in the below example, which will set the member name to point towards the location of "hello world" and func to have the adress of my_function . 可以使用{}设置对象的成员,如下例所示,它将成员name设置为指向"hello world"的位置,并将func为具有my_function的地址。

luaL_reg obj = {"hello world", my_function};

The syntax shown in the previous snippet can also be used when initializing members of an array. 在初始化数组成员时,也可以使用上一个代码段中显示的语法。 In the below snippet a array of const luaL_reg instances is set to contain two objects, the first one having name = "base" and func set to luaopen_base . 在下面的代码片段中,const luaL_reg实例数组被设置为包含两个对象,第一个具有name = "base"并且func设置为luaopen_base

To make things clear; 使事情清楚; the below is not a 2D array, but an array of const luaL_reg initialized using {} to set the members of each instance. 下面不是 2D数组,而是使用{}初始化的const luaL_reg数组来设置每个实例的成员。

static const luaL_reg lualibs[] =
{
  { "base",       luaopen_base },
  { NULL,         NULL }
};

The last element is used to simplify iterating our array, setting both members to NULL makes it easy to see when we have reached the last element. 最后一个元素用于简化迭代我们的数组,将两个成员设置为NULL可以很容易地看到我们何时到达最后一个元素。

The loop in the upcoming snippet takes advantage of this. 即将到来的片段中的循环利用了这一点。 As long as the member func isn't equal to NULL we haven't gotten to the end of our array. 只要成员func不等于NULL我们就没有到达数组的末尾。

for (lib = lualibs; lib->func != NULL; lib++) {
  lib->func(l);
  lua_settop(l, 0);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM