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

查看:149
本文介绍了C ++或C中的foo(void)和foo()之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下两个函数定义:

Consider these two function definitions:

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),我们可以在两种语言中实现相同的解释,并使标头变为多语言(尽管我们通常需要对标头做更多的事情才能使它们真正成为跨语言;也就是说,将它们包装在extern "C"(如果我们正在编译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天全站免登陆