问题描述
考虑这两个函数的定义:
Consider these two function definitions:
void foo() {}
void foo(void) {}
有没有这两者之间有什么区别?如果不是,为什么是无效
的说法呢?审美的原因?
Is there any difference between these two? If not, why is the void
argument there? Aesthetic reasons?
推荐答案
的主要原因是为了实现被C和C之间共享的头一贯间pretation ++。
The main reason is to achieve consistent interpretation of headers that are shared between C and C++.
在 C :结果无效美孚()
表示功能富
服用不明类型的参数数目不详结果无效美孚(无效)
表示功能富
不接受参数
In C:void foo()
means "a function foo
taking an unspecified number of arguments of unspecified type"void foo(void)
means "a function foo
taking no arguments"
在 C ++ :结果无效美孚()
表示功能富
不接受参数结果无效美孚(无效)
表示功能富
不接受参数
In C++:void foo()
means "a function foo
taking no arguments"void foo(void)
means "a function foo
taking no arguments"
通过写富(无效)
,因此,我们实现了跨越两种语言相同间$ P $ ptation,使我们的头多语言(尽管我们通常需要做一些更多的事情的头,使他们真正的跨语言的,即,在的externC
如果我们编译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之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!