C++ 或 C 中的 foo(void) 和 foo() 之间有区别吗? [英] Is there a difference between foo(void) and foo() in C++ or C?
本文介绍了C++ 或 C 中的 foo(void) 和 foo() 之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑这两个函数定义:
void foo() { }
void foo(void) { }
这两者有什么区别吗?如果没有,为什么 void
参数在那里?审美原因?
Is there any difference between these two? If not, why is the void
argument there? Aesthetic reasons?
推荐答案
In C:
void foo()
表示一个函数foo
接受未指定数量的未指定类型的参数"void foo(void)
表示一个不带参数的函数foo
"
void foo()
means "a functionfoo
taking an unspecified number of arguments of unspecified type"void foo(void)
means "a functionfoo
taking no arguments"
在 C++ 中:
void foo()
表示一个没有参数的函数foo
"void foo(void)
表示一个不带参数的函数foo
"
void foo()
means "a functionfoo
taking no arguments"void foo(void)
means "a functionfoo
taking no arguments"
通过编写 foo(void)
,因此,我们实现了跨两种语言的相同解释并使我们的标头多语言(尽管我们通常需要对标头做一些更多的事情来使它们真正交叉-language;即,如果我们正在编译 C++,则将它们包装在 extern "C"
中).
By writing foo(void)
, therefore, we achieve the same interpretation across both languages and make our headers multilingual (though we usually need to do some more things to the headers to make them truly cross-language; namely, wrap them in an extern "C"
if we're compiling C++).
这篇关于C++ 或 C 中的 foo(void) 和 foo() 之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文