C++ 或 C 中的 foo(void) 和 foo() 有区别吗? [英] Is there a difference between foo(void) and foo() in C++ or C?

查看:35
本文介绍了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?

推荐答案

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++).

这篇关于C++ 或 C 中的 foo(void) 和 foo() 有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆