[英]I am trying to understand a specific function pointer and assignment
I am reading the source code of the video game “Rise of the Triad: Dark War”, and I came across a line of code I don't understand. 我正在阅读视频游戏“黑社会的崛起:黑暗战争”的源代码,但遇到了一些我不理解的代码 。
void (*USL_MeasureString)(char *, int *, int *, font_t *) = (void (*)(char *, int *, int *, font_t *))VW_MeasurePropString, (*USL_DrawString)(char *) = VWB_DrawPropString;
As far as I understand it void (*USL_MeasureString)(char *, int *, int *, font_t *)
means *USL_MeasureString
is a function pointer that takes char *, int *, int *, font_t *
as parameters and returns nothing because of void
. 据我了解,
void (*USL_MeasureString)(char *, int *, int *, font_t *)
意味着*USL_MeasureString
是一个函数指针,它以char *, int *, int *, font_t *
作为参数,并且不返回任何值,因为void
。
But then, I am confused about the rest of the code. 但是随后,我对其余的代码感到困惑。
What does it means exactly? 到底是什么意思?
Best regards. 最好的祝福。
Let's break this long line into three parts: 让我们将这条长线分成三部分:
void
(*USL_MeasureString)(char *, int *, int *, font_t *) = (void (*)(char *, int *, int *, font_t *))VW_MeasurePropString,
(*USL_DrawString)(char *) = VWB_DrawPropString;
I believe it's easier to understand now. 我相信现在更容易理解。 As you've correctly figured out,
USL_MeasureString
is defined as a function pointer, and the RHS (Right-Hand Side) of the assignment is a type cast applied to the word VW_MeasurePropString
. 如您
USL_MeasureString
, USL_MeasureString
被定义为函数指针,而赋值的RHS(右侧)是应用于单词VW_MeasurePropString
的类型VW_MeasurePropString
。
The second part appears tricky on its own, but once you get to know how the following line works, you'll know it's the same: 第二部分看起来很棘手,但是一旦您了解以下行的工作原理,您就会知道它是相同的:
char (*a) = (char *)"123", (*b) = "xyz";
Here, both a
and b
are pointers to char
, so you can think of char
as a "base type" for the whole line of variable definitions. 这里,
a
和b
都是char
指针,因此您可以将char
视为整行变量定义的“基本类型”。 And of course you can rewrite it as: 当然,您可以将其重写为:
char (*a) = (char *)"123";
char (*b) = "xyz";
So the part after the comma in your original code can be rewritten as (where void
is the "base type"): 因此,原始代码中逗号后的部分可以重写为(其中
void
是“基本类型”):
void (*USL_DrawString)(char *) = VWB_DrawPropString;
As you can see, it's just the definition of another function pointer with a different argument list and no type casting. 如您所见,它只是具有不同参数列表且没有类型转换的另一个函数指针的定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.