在C ++中的变量声明中使用struct关键字 [英] using struct keyword in variable declaration in C++

查看:209
本文介绍了在C ++中的变量声明中使用struct关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个感觉这可能与C语法相关,但我开始我的编程生活与C + +,所以我不知道。

I have a feeling this may be related to C syntax, but I started my programming life with C++ so I am not sure.

基本上我已经看到这个:

Basically I have seen this:

struct tm t;
memset( &t, 0, sizeof(struct tm) );

我对这种语法感到困惑,通常我希望上面的代码看起来像这样:

I am a bit confused with this syntax, as normally I would expect the above to instead look like this:

tm t;
memset( &t, 0, sizeof(tm) );

两者之间的区别是什么,为什么使用前者?

What is the difference between the two, and why is the former used instead?

我所指的结构 tm code> wchar.h ,定义如下:

The structure tm that I am referring to is in wchar.h, and the definition is as follows:

struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };


推荐答案

简单的回答是 struct 关键字,以限制标识符 tm 仅查找用户定义的类类型

The simple answer is that the struct keyword there is present to restrict the lookup of the identifier tm to only user defined class types. It is probably left for compatibility with C, where it is required.

与其他人说的相反,没有这样的东西,如 auto-typedef ,C和C ++也不会如何管理用户定义类型的标识符。唯一的区别是查找。

Contrary to what others say, there is no such thing as auto-typedef, nor do C and C++ differ with respect to how the identifiers for user defined types are managed. The only difference is in lookup.

您可以阅读更多这里

这篇关于在C ++中的变量声明中使用struct关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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