C++无效函数声明
Possible Duplicate:
C++ Why put void in params?
What's the difference between these two declarations and which is used more commonly?
void function1();
and
void function2( void );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在C++中没有什么区别,它被明确定义为代表0个参数。
然而,它在 C 中确实是这样。带有
(void)
的函数意味着没有参数,而()
意味着带有任意数量的参数。来自 http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fparam_decl.htm
There is no difference in C++, where it is well defined that it represents 0 parameters.
However it does make one in C. A function with
(void)
means with no parameter, whereas()
means with any number of parameters.From http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fparam_decl.htm
C++
中没有区别。第二个声明只是明确表示该函数不带参数。
第二个在
C
中更常用,第一个是在C++
中更常见。C
的情况有所不同,因为:使用
(void)
,您指定该函数没有参数,而使用()
,您指定参数未指定(参数数量未知)。但是,如果它不是函数声明而是函数定义,那么即使在
C
中,它也与(void)
相同。There is no difference in
C++
.The second declaration just explicitly says that function takes no parameter.
Second is more commonly used in
C
, First is the one that is more common inC++
.There is a difference in case of
C
because:With
(void)
, you're specifying that the function has no parameters, while with()
you are specifying that the parameters are unspecified(unknown number of arguments).However, if it was not a function declaration but a function definition, then even in
C
it is same as(void)
.没有什么区别。我想说第一个更常见、更清晰、更简洁。
There is no difference. I'd say the first one is more common, clear and concise.
在C++中,没有区别,只是为了C兼容性而保留第二种形式。 C++ 中首选第一种形式。
在C语言中,它们意味着不同的东西。
第一种形式指定一个采用未知数量参数的函数,第二种形式指定采用零个参数的函数。
In C++, there is no difference, and the second form is only retained for C compatibility. The first form is preferred in C++.
In C, they mean different things.
The first form specifies a function which takes an unknown number of arguments, and the second is a function taking zero arguments.
一些非常旧的(非标准)C 编译器可能会抱怨第一个,所以第二个应该更可移植。
除此之外,没有什么区别。
第一个在用户代码中更常用,原因很简单,因为它更短。
Some very old (non-standard) C compiler might complain about the first one, so the second one should be more portable.
Apart from that, there is no difference.
The first one is used more commonly in user code, quite simply because it's shorter.
实际上没有区别。如果您没有任何参数传递给方法用户 void 或空括号。请注意,它只是传递参数。如果您的方法没有任何返回值,则必须使用 void 关键字。第一个是在 C# 中更常见
actually there is no difference .if you have not any parameters to pass to the method user void or empty parentheses .notice that it just fro passed parameters.if your method has not any returned value you have to use void keyword.the first one is more common in C#