更新时间:2022-10-23 17:08:33
无效美孚(无效);
这是说,在C没有参数的正确的方法,而且它也适用于C ++。
不过:
无效美孚();
意味着C和C ++不同的事情!在C语言中的意思是可以采取任何数量的未知类型参数,并在C ++中它的意思一样富(无效)
。
变量参数列表功能,本质上是未类型安全,应尽量避免使用。
What is better: void foo()
or void foo(void)
?
With void it looks ugly and inconsistent, but I've been told that it is good. Is this true?
Edit: I know some old compilers do weird things, but if I'm using just GCC, is void foo()
Ok? Will foo(bar);
then be accepted?
void foo(void);
That is the correct way to say "no parameters" in C, and it also works in C++.
But:
void foo();
Means different things in C and C++! In C it means "could take any number of parameters of unknown types", and in C++ it means the same as foo(void)
.
Variable argument list functions are inherently un-typesafe and should be avoided where possible.