C++函数声明

发布于 2024-12-09 12:11:28 字数 187 浏览 2 评论 0原文

我正在研究一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-12-16 12:11:28

在这种情况下,void 意味着该函数不带任何参数。

另外 - 语法错误,您可能指的是 {} 括号,而分号放在了错误的位置。

int area (void){ return (x*y); }

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.

int area (void){ return (x*y); }
潦草背影 2024-12-16 12:11:28

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.

錯遇了你 2024-12-16 12:11:28

该代码无效。应该是(也更改了空格以使其更清晰,但问题不是它)

int area(void) { return (x * y); }

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) { return (x * y); }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文