C++函数声明
我正在研究一些 C++ 教程,并在类中遇到了函数声明
class CRectangle {
int x, y;
public:
int area (void) {return x*y;}
};
现在我想知道 int 区域后面的 void
有什么用?
I was looking into some C++ tutorial and encountered a function declaration inside the class
class CRectangle {
int x, y;
public:
int area (void) {return x*y;}
};
Now I am wondering what is the use of void
after int area?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,
void
意味着该函数不带任何参数。另外 - 语法错误,您可能指的是
{}
括号,而分号放在了错误的位置。void
in this case means that the function doesn't take any parameters.Also - syntax error, you probably meant
{}
brackets and you had the semicolon in the wrong place.void 参数类型在 C++ 中是不必要的。使用空参数列表声明的函数是等效的。它合法的原因是允许 C 代码无错误地编译。
void 参数类型在 C 中是必需的,因为使用空参数列表声明的函数可以接受任意数量的任意类型参数。这是 ANSI C 之前的残余,也称为 K&R C。K&RC 不需要函数原型。
The void parameter type is unnecessary in C++. A function declared with an empty argument list is equivalent. The reason it's legal is to allow C code to compile without error.
The void parameter type is necessary in C because a function declared with an empty argument list accepts any number of any typed arguments. This is a remnant of pre-ANSI C, also known as K&R C. K&R C did not require function prototypes.
该代码无效。应该是(也更改了空格以使其更清晰,但问题不是它)
int area(void)
是成员函数签名 -int
是返回类型,(void)
表示空参数列表。它是 C 主义,不应该在 C++ 中使用——int area()
意味着同样的事情。This code is invalid. It should be (also changed whitespace to be more clear, but the issue is not about it)
int area(void)
is the member function signature —int
is the returned type,(void)
means an empty argument list. It's a C-ism, and shouldn't be used in C++ —int area()
means the same thing.