C++ 或 C 中的 foo(void) 和 foo() 有区别吗?区别、foo、void

由网友(万人空巷i)分享简介:考虑这两个函数定义:void foo() { }void foo(void) { }这两者有什么区别吗?如果不是,为什么有 void 参数?审美原因?Is there any difference between these two? If not, why is the void argument there?...

考虑这两个函数定义:

void foo() { }

void foo(void) { }

这两者有什么区别吗?如果不是,为什么有 void 参数?审美原因?

Is there any difference between these two? If not, why is the void argument there? Aesthetic reasons?

推荐答案

在C中:

void foo() 表示一个函数 foo 采用未指定数量的未指定类型的参数"void foo(void) 表示一个函数 foo 不带参数" 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++ 中:

void foo() 表示一个函数 foo 不带参数"void foo(void) 表示一个函数 foo 不带参数" void foo() means "a function foo taking no arguments" void foo(void) means "a function foo 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++).

阅读全文

相关推荐

最新文章