简体   繁体   English

在c ++中,VOID和void有区别吗?

[英]Is there a difference in VOID and void, in c++?

I've read some other questions similar to this, but I still don't know if there is a difference in terms of usage between "VOID" and "void". 我读过其他一些与此类似的问题,但是我仍然不知道“ VOID”和“ void”在用法上是否有区别。

I feel uncomfortable using VOID without knowing the exact difference with void. 在不知道void的确切区别的情况下,使用VOID感到不舒服。

Any comforting advice is greatly appreciated. 任何安慰的建议,我们将不胜感激。

void is a keyword reserved by the language, but VOID is not. void是语言保留的关键字,但VOID不是。 It is defined in "winnt.h" as follows: 它在“ winnt.h”中定义如下:

#ifndef VOID
#define VOID void

So they are essentially the same and can be used interchangeably. 因此它们本质上是相同的,可以互换使用。 Sometimes one is used in place of the other for conventions' sake, but it doesn't matter which one you use. 有时,为了约定,使用一个代替另一个,但是使用哪个无关紧要。 The preprocessor will end up replacing VOID with void in your code anyway because that is what #define does. 无论如何,预处理器最终都会用代码中的void替换VOID ,因为#define就是这样做的。

Actually a lot of keywords are "typedefed" in Windows to their capital. 实际上,许多关键字在Windows中被“ typedef定义”为大写。 I think it is just to ensure compatibility between 8-bit, 16-bit and 32-bit Windows. 我认为这只是为了确保8位,16位和32位Windows之间的兼容性。 Why they did that with void though... That's just Windows being weird. 为什么他们这样做却是void ……那只是Windows太奇怪了。 (As far as I know) (我所知道的)

The difference is that void is part of the language and VOID is not. 区别在于, void是语言的一部分,而VOID不是。 If you look at the output of the preprocessor for one of your source files containing VOID , you should be able to find a line that contains a line something like: 如果查看包含VOID的源文件之一的预处理器的输出,则应该能够找到包含以下内容的行:

typedef void VOID;

Which should make you feel better. 这应该会让您感觉更好。

Edit: I see from your comment above that there's a #define VOID void line. 编辑:我从您上面的评论中看到有#define VOID void行。 In that case, you know they're the same, but you likely won't see any VOID in the preprocessor output. 在那种情况下,您知道它们是相同的,但是您可能在预处理器输出中看不到任何VOID What you will see, is that everywhere in your code that you used VOID , the preprocessed output will have void instead. 看到的是,在代码中所有使用VOID ,预处理后的输出将VOID void

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

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