教你理解复杂的C-C++声明(下)

来源:互联网 发布:qbittorrent ubuntu 编辑:程序博客网 时间:2024/05/05 09:24

教你理解复杂的C-C++声明(下)

typedef的妙用 

typedef给你一种方式来克服“*只适合于变量而不适合于类型”的弊端。你可以如下使用typedef: 

 

typedef char *PCHAR; 

PCHAR p,q; 

 

这里的p和q都被声明为指针。(如果不使用typedef,q将被声明为一个char变量,这跟我们的第一眼感觉不太一致!)下面有一些使用typedef的声明,并且给出了解释: 

 

typedef char * a;// a is a pointer to a char 

 

typedef ab();   // b is a function thatreturns 

          // a pointer to a char 

 

typedef b *c;   // c is a pointer to a function 

          // that returns a pointer to achar 

 

typedef cd();   // d is a function returning 

          // a pointer to a function 

          // that returns a pointer to achar 

 

typedef d *e;   // e is a pointer to a function 

          // returning a pointer to a 

          // function that returns a 

          // pointer to a char 

 

e var[10];     // var is an array of 10 pointers to 

          // functions returning pointersto 

          // functions returning pointers tochars. 

 

typedef经常用在一个结构声明之前,如下。这样,当创建结构变量的时候,允许你不使用关键字struct(在C中,创建结构变量时要求使用struct关键字,如struct tagPOINT a;而在C++中,struct可以忽略,如tagPOINT b)。 

 

typedef structtagPOINT 

  int x; 

  int y; 

}POINT; 

 

POINT p; /* ValidC code */ 

//------------------------------------------------------------------------


函数指针  

函数指针可能是最容易引起理解上的困惑的声明。函数指针在DOS时代写TSR程序时用得最多;在Win32和X-Windows时代,他们被用在需要回调函数的场合。当然,还有其它很多地方需要用到函数指针:虚函数表,STL中的一些模板,Win NT/2K/XP系统服务等。让我们来看一个函数指针的简单例子: 

 

int(*p)(char); 

 

这里p被声明为一个函数指针,这个函数带一个char类型的参数,并且有一个int类型的返回值。另外,带有两个float类型参数、返回值是char类型的指针的指针的函数指针可以声明如下: 

 

char **(*p)(float, float); 

 

那么,带两个char类型的const指针参数、无返回值的函数指针又该如何声明呢?参考如下: 

 

void *(*a[5])(char * const, char * const); 

 

“右左法则”[重要!!!] 

 

The right-leftrule: Start reading the declaration from the innermost parentheses, go right,and then go left. When you encounter parentheses, the direction should bereversed. Once everything in the parentheses has been parsed, jump out of it.Continue till the whole declaration has been parsed. 

 

这是一个简单的法则,但能让你准确理解所有的声明。这个法则运用如下:从最内部的括号开始阅读声明,向右看,然后向左看。当你碰到一个括号时就调转阅读的方向。括号内的所有内容都分析完毕就跳出括号的范围。这样继续,直到整个声明都被分析完毕。 

 

对上述“右左法则”做一个小小的修正:当你第一次开始阅读声明的时候,你必须从变量名开始,而不是从最内部的括号。 

 

下面结合例子来演示一下“右左法则”的使用。 

 

int * (* (*fp1)(int) ) [10]; 

 

阅读步骤: 

1. 从变量名开始-------------------------------------------- fp1 

2. 往右看,什么也没有,碰到了),因此往左看,碰到一个* ------ 一个指针 

3. 跳出括号,碰到了(int) ----------------------------------- 一个带一个int参数的函数 

4. 向左看,发现一个* --------------------------------------- (函数)返回一个指针 

5. 跳出括号,向右看,碰到[10] ------------------------------ 一个10元素的数组 

6. 向左看,发现一个* --------------------------------------- 指针 

7. 向左看,发现int----------------------------------------- int类型 

 

总结:fp1被声明成为一个函数的指针,该函数返回指向指针数组的指针. 

 

再来看一个例子: 

int *( *(*arr[5])())(); 

 

阅读步骤: 

1. 从变量名开始-------------------------------------------- arr 

2. 往右看,发现是一个数组 ---------------------------------- 一个5元素的数组 

3. 向左看,发现一个* --------------------------------------- 指针 

4. 跳出括号,向右看,发现() -------------------------------- 不带参数的函数 

5. 向左看,碰到*------------------------------------------- (函数)返回一个指针 

6. 跳出括号,向右发现() ------------------------------------ 不带参数的函数 

7. 向左,发现*--------------------------------------------- (函数)返回一个指针 

8. 继续向左,发现int ---------------------------------------int类型 

 

总结:arr被声明成为一个函数的数组指针,该函数返回指向函数指针的指针。?? 

 

还有更多的例子: 

float ( * ( *b())[] )();      

              // b is a function that returnsa 

       // pointer to an array of pointers 

       // to functions returning floats. 

 

void * ( *c) (char, int (*)());       

              // c is a pointer to a functionthat takes 

       // two parameters: 

       //  a char and a pointer to a 

       //  function that takes no 

      //  parameters and returns 

     //  an int 

     // and returns a pointer to void. 

 

void ** (*d) (int&,  char **(*)(char *, char**));  

              // d is a pointer to a functionthat takes 

      // two parameters: 

   //   areference to an int and a pointer 

   //  to a function that takes two parameters: 

   //   a pointer to a char and a pointer 

   //   to a pointer to a char 

   //  and returns a pointer to a pointer 

 //   toa char 

  // and returns a pointer to a pointer tovoid 

 

float ( * ( * e[10])(int &) ) [5];             

              // e is an array of 10 pointersto 

       // functions that take a single 

       // reference to an int as anargument 

       // and return pointers to 

       // an array of 5 floats.   


0 0