c++ - 在 C++ 中的变量声明中使用 struct 关键字

标签 c++ struct

我感觉这可能与 C 语法有关,但我的编程生涯是从 C++ 开始的,所以我不确定。

基本上我已经看到了:

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

我对这种语法有点困惑,因为通常我希望上面看起来像这样:

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

两者有什么区别,为什么用前者代替?

更新

我所指的结构tmwchar.h中,定义如下:

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 的查找限制为仅 用户定义的类类型。它可能是为了与 C 兼容而留下的。

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

您可以阅读更多 here

关于c++ - 在 C++ 中的变量声明中使用 struct 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7729646/

相关文章:

c++ - 如何跨平台将 char* 转换为 long long(64 位)?

无法理解结构初始化

html - 如何将 html 数据移动到 Golang 中的结构

c - 不同数据类型中的指针行为

c++ - 在Qt中保存下载的文件

c++ - 传递 shared_array<T> 参数

c - 简单的 C 结构 : Trouble storing a char * [] into a struct in C. 不兼容的类型

c++ - 处理结构类型的数组

c++ - 无法将光标位置输出到调试字符串 (WIN32)

c++ - : ascii or binary? 哪种格式的STL文件读取速度更快