c/c++语言开发共享有关typedefs的帮助 – 基本的c / c ++

我一直在阅读一些代码并且发现了一个不知何故扰乱了我的声明。

typedef GLfloat vec2_t[2]; typedef GLfloat vec3_t[3]; 

从我的角度来看,如

 typedef unsigned long ulong; 

意味着ulong被认为是指无符号长
现在,下面的陈述是否意味着vec2_t [2]相当于GLfloat

 typedef GLfloat vec2_t[2]; 

最有可能的,可能不是预期的含义。 如果有人为我解决这个问题,我将不胜感激。 谢谢

    基本上, typedef具有与普通C声明完全相同的格式,但它为该类型引入了另一个名称,而不是该类型的变量。

    在您的示例中,如果没有typedef,则vec2_t将是两个GLfloat的数组。 使用typedef意味着vec2_t是“两个GLfloat的数组”类型的新名称。

     typedef GLfloat vec2_t[2]; 

    这意味着这两个声明是等效的:

     vec2_t x; GLfloat x[2]; 

    C声明语法可能令人困惑,但您只需要一个简单的规则: 从里到外读取 。 完成后,只需了解typedef为类型创建另一个名称(别名)。

    内部是声明的标识符(或者如果丢失则会去的地方)。 例子:

     T a[2]; // array (length 2) of T T* a[2]; // array (length 2) of pointer to T ([] before *) T (*p)[2]; // pointer to array (length 2) of T (parens group) T f(); // function returning T T f(int, char*); // function of (int, pointer to char) returning T T (*p)(int); // pointer to function of (int) returning T T (*f(char, T(*)[2]))(int); // f is a function of (char, // pointer to array (length 2) of T) // returning a pointer to a function of (int) // returning T typedef T (*F(char, T(*)[2]))(int); // F is the type: // function of (char, // pointer to array (length 2) of T) // returning a pointer to a function of (int) // returning T // (yes, F is a function type, not a pointer-to-function) F* p1 = 0; // pointer to F T (*(*p2)(char, T(*)[2]))(int) = 0; // identical to p1 from the previous line 

    在您的情况下, vec2_t是2个GLfloats的数组, vec3_t是3个GLfloats的数组。 然后你可以做以下事情:

     vec2_t x; // do stuff with x[0] and x[1] 

    如果要为数组类型GLfloat[2]创建typedef-name vec2_t ,则正确的语法不会

     typedef GLfloat[2] vec2_t; 

    (初学者可能会期待),而是

     typedef GLfloat vec2_t[2]; 

    即,这里的语法的一般结构,正如已经说过的那样,在变量声明中是相同的。

    这意味着vec2_t是2 GLfloat的数组, vec3_t是3的数组。

    更多信息

    如果C语法允许将其写为,则意图会更清晰

     typedef GLfloat[2] vec2_t; typedef GLfloat[3] vec3_t; 

    但那不是有效的语法。

      以上就是c/c++开发分享有关typedefs的帮助 – 基本的c / c ++相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/560450.html

      (0)
      上一篇 2021年1月28日
      下一篇 2021年1月28日

      精彩推荐