在没有参数的函数中使用void? [英] Using void in functions without parameter?

查看:834
本文介绍了在没有参数的函数中使用void?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个没有参数的函数中,在C ++中使用 void ,例如:

  class WinMessage 
{
public:
BOOL Translate(void);
};

是多余的,您可能只需编写 Translate(); < code $。

我自己通常包含它,因为当代码完成支持IDE显示 void ,因为它确保了该函数绝对不会有参数。



我的问题是,将 void 添加到无参数函数是一种好的做法吗?在现代代码中是否应该鼓励它?

解决方案

  void f(void); 

等同于:

  void f(); 

第一个样式仍然可以合法编写的事实可归因于C.

n3290§C.1.7(C ++和ISO C兼容)指出:
$ b blockquote>

更改:在C ++ ,一个用空参数列表声明的函数需要
没有参数。



在C中,一个空参数列表表示数字和
类型


$ b $ p $ int f( ); //表示C ++中的int f(void)
// int f(未知)中的C


在C语言中,避免不需要的未知含义是有意义的。在C ++中,这是多余的。

简短的回答:在C ++中,这是C编程过多的后遗症。这就把它放在不要这样做,除非你真的必须在我的视图中支持C ++。


In C++ using void in a function with no parameter, for example:

class WinMessage
{
public:
    BOOL Translate(void);
};

is redundant, you might as well just write Translate();.

I, myself generally include it since it's a bit helpful when code-completion supporting IDEs display a void, since it ensures me that the function takes definitely no parameter.

My question is, Is adding void to parameter-less functions a good practice? Should it be encouraged in modern code?

解决方案

In C++

void f(void);

is identical to:

void f();

The fact that the first style can still be legally written can be attributed to C.
n3290 § C.1.7 (C++ and ISO C compatibility) states:

Change: In C++, a function declared with an empty parameter list takes no arguments.

In C, an empty parameter list means that the number and type of the function arguments are unknown.

Example:

int f(); // means int f(void) in C++
         // int f( unknown ) in C

In C, it makes sense to avoid that undesirable "unknown" meaning. In C++, it's superfluous.

Short answer: in C++ it's a hangover from too much C programming. That puts it in the "don't do it unless you really have to" bracket for C++ in my view.

这篇关于在没有参数的函数中使用void?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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