为什么括号在函数指针声明中很重要?

发布于 2024-10-07 10:31:27 字数 304 浏览 0 评论 0原文

我不明白为什么下面的声明被接受:

typedef void    (*_tStandardDeclaration)(LPVOID);

而下面的声明不被接受:

typedef void    *_tDeclarationWithoutParenthesis(LPVOID);
typedef void*   _tAlternateDeclaration(LPVOID);

我正在使用MSVC6(我知道它已经过时且不标准,但需要它来维持每年千万级的收入系统:/)

I don't understand why the declaration below is accepted:

typedef void    (*_tStandardDeclaration)(LPVOID);

while the following doesn't:

typedef void    *_tDeclarationWithoutParenthesis(LPVOID);
typedef void*   _tAlternateDeclaration(LPVOID);

I am using MSVC6 (I know it's obsolete and non-standard, but it's needed to maintain a yearly tenth-million revenue system :/ )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北陌 2024-10-14 10:31:27

默认情况下,指针符号绑定到类型,因此函数指针需要括号来指示指针实际上位于名称上,而不是返回类型上。

The pointer symbol binds to the type by default, so the function pointer needs the parenthesis to indicate that the pointer is actually on the name and not on the return type.

尴尬癌患者 2024-10-14 10:31:27

如果没有括号,您将声明一个返回 void* 的函数,而不是指向返回 void 的函数的指针。

Without the parentheses, you're declaring a function returning a void*, not a pointer to a function returning void.

謸气贵蔟 2024-10-14 10:31:27

下面的代码在编译器设置为 fussy 的情况下被 MacOS X 10.6.5 上的 GCC 4.2.1 毫不犹豫地接受:

c++ -Wall -Wextra -c xx.cpp

代码:

typedef void *LPVOID;

typedef void    (*_tStandardDeclaration)(LPVOID);

typedef void    *_tDeclarationWithoutParenthesis(LPVOID);
typedef void*   _tAlternateDeclaration(LPVOID);

第一个给出一个指向返回 void 的函数的指针;后两者是等效的(空格没有区别),并为您提供一个类型“返回指向 void 的指针的函数(采用 LPVOID 参数)”。

您可以使用它们来声明函数指针:

typedef _tDeclarationWithoutParenthesis *_tFunctionPointer;

很有趣,不是吗...

The code below is accepted without a witter by GCC 4.2.1 on MacOS X 10.6.5 with the compiler set to fussy:

c++ -Wall -Wextra -c xx.cpp

Code:

typedef void *LPVOID;

typedef void    (*_tStandardDeclaration)(LPVOID);

typedef void    *_tDeclarationWithoutParenthesis(LPVOID);
typedef void*   _tAlternateDeclaration(LPVOID);

The first gives a pointer to a function returning void; the latter two are equivalent (spacing makes no difference) and give you a type that is 'function (taking LPVOID argument) that returns pointer to void'.

You can use them to declare function pointers:

typedef _tDeclarationWithoutParenthesis *_tFunctionPointer;

Fun, isn't it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文