为什么C&需要QUOT;结构"关键字,而不是C ++? [英] Why does C need "struct" keyword and not C++?

查看:172
本文介绍了为什么C&需要QUOT;结构"关键字,而不是C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有点困惑什么是怎么回事:

I've always been a little confused about what's going on here:

#include <stdio.h>

int main() {  
    timeval tv;
    tv.tv_sec = 1;

    for (;;) {
        select(0, 0, 0, 0, &tv);
        printf("%s\n", "Hello World!");
    }
}

很抱歉,如果无法编译,只是写它作为一个简单的例子。

Sorry if that doesn't compile, just wrote it as a quick example.

code这样就不会在GCC编译,除非我添加关键字结构之前使用timeval结构中。 G ++,另一方面其处理罚款的。

Code like this won't compile under gcc unless I add the keyword struct prior to the use of the struct timeval. g++ on the other hand handles it fine as is.

这是如何C和C ++手柄结构之间的差异还是仅仅是在编译器有区别吗? (我很C ++为导向,并在这样的线路在C中使用的结构一直有点困惑我)。

Is this a difference between how C and C++ handle structures or is it just a difference in the compilers? (I'm very C++ oriented, and the use of struct in C on lines like this has always somewhat baffled me).

推荐答案

在语法上都请客结构几乎相同。只有C ++增加了一个额外的规则,允许省略结构(和)的关键字,如果没有歧义。

Syntactically both treat struct almost the same. Only C++ has added an extra rule that allows to omit the struct (and class) keyword if there is no ambiguity.

如果有歧义,也是C ++需要在一些地方结构关键字。一个有名的例子是统计在POSIX系统中有一个 struct stat中和功能统计

If there is ambiguity, also C++ requires the struct keyword in some places. A notorious example is stat on POSIX systems where there is a struct stat and a function stat.

这篇关于为什么C&需要QUOT;结构&QUOT;关键字,而不是C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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