为什么在方法参数列表中添加void [英] Why add void to method parameter list
问题描述
我看到了以下签名的方法:
I've seen methods with the following signature:
void foo (void);
他们没有参数,但我想知道这样做是否有用。
They take no argument, however I'm wondering whether doing this is useful or not. Is there a reason why you would want to do it?
推荐答案
C ++ 03的标准说明(强调我的) / p>
The C++03 standard says (emphasis mine):
8.3.5.2
8.3.5.2
参数声明子句be
指定,以及它们的处理,当调用该函数时。 [注:
参数声明子句用于转换在函数调用上指定的参数
;见5.2.2。 ] 如果
parameter-declaration-clause为空,则该函数不接受
参数
The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the function is called. [Note: the parameter-declaration-clause is used to convert the arguments specified on the function call; see 5.2.2. ] If the parameter-declaration-clause is empty, the function takes no arguments.
这意味着如果你正在与编译器交谈只是一个品味问题。
This means that if you are talking to the compiler it's just a matter of taste.
如果你正在编写代码,将被其他人阅读, C ++的做事方式是
If you are writing code that will be read by others, then the C++ way of doing things is
void foo();
另一种形式仅在与C兼容的情况下仍然有效, em>两个签名之间的差异。
The other form remains valid only for reasons of compatibility with C, where there was a difference among the two signatures.
这篇关于为什么在方法参数列表中添加void的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!