结构体tag和name,为什么声明为name的局部变量会编译?

发布于 2024-10-05 19:19:51 字数 373 浏览 7 评论 0原文

在我最近看到的一些代码中,有一个如下定义的结构:

typedef struct tagMyStruct {
    int numberOne;
    int numberTwo;
} MYSTRUCT;

据我了解,tagMyStruct是新的数据类型,MYSTRUCT是在那里创建的变量。

在另一个地方,它是这样使用的:

MYSTRUCT *pStruct = new MYSTRUCT;

并且它在 Visual Studio 2010 中编译得很好。这如何是有效的 C++?我认为 MYSTRUCT 是一个变量而不是类型?

In some code I saw recently there was a structure defined like this:

typedef struct tagMyStruct {
    int numberOne;
    int numberTwo;
} MYSTRUCT;

The way I understand this, tagMyStruct is the new data type and MYSTRUCT is a variable that is created right there.

At another place, this was used like this:

MYSTRUCT *pStruct = new MYSTRUCT;

and it compiled fine with Visual Studio 2010. How is that valid C++? I thought MYSTRUCT was a variable and not a type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

GRAY°灰色天空 2024-10-12 19:19:51

不。tagMyStruct 是结构的名称。在 C 中,与 C++ 不同,每次使用结构类型时都必须显式使用 struct 关键字。例如,

tagMyStruct x; //error
struct tagMyStruct x; //OK

为了避免始终写入 struct,将 struct tagMyStruct typedef 转换为 MYSTRUCT。现在你可以写

MYSTRUCT x; //ok, same as struct tagMyStruct x;

你所认为的(变量定义),而无需使用 typedef 关键字,就像这样顺便

struct tagMyStruct {
    int numberOne;
    int numberTwo;
} MYSTRUCT;

说一句,

MYSTRUCT pStruct = new MYSTRUCT; //error cannot convert MYSTRUCT* to MYSTRUCT

无论如何,这都不是有效的 C 或 C++。也许你的意思是

MYSTRUCT* pStruct = new MYSTRUCT; //valid C++ (invalid C - use malloc instead of new in C)

hth

No. tagMyStruct is the name of the struct. In C, unlike C++, you must explicitly use the struct keyword every time you use the struct type. For example

tagMyStruct x; //error
struct tagMyStruct x; //OK

To avoid writing struct all the time, struct tagMyStruct is typedef'd to MYSTRUCT. Now you can write

MYSTRUCT x; //ok, same as struct tagMyStruct x;

What you thought this was (a variable definition) would be without the typedef keyword, like this

struct tagMyStruct {
    int numberOne;
    int numberTwo;
} MYSTRUCT;

BTW

MYSTRUCT pStruct = new MYSTRUCT; //error cannot convert MYSTRUCT* to MYSTRUCT

is not valid C or C++ anyway. Maybe you mean

MYSTRUCT* pStruct = new MYSTRUCT; //valid C++ (invalid C - use malloc instead of new in C)

hth

十级心震 2024-10-12 19:19:51
struct tagMyStruct { ... };

定义了一个名为 tagMyStruct 的新 C++ 类型(类)。

struct { ... } MYSTRUCT;

使用给定的结构定义一个名为 MYSTRUCT 的变量。

typedef struct { ... } MYSTRUCT;

定义了一个名为 MYSTRUCT 的 typedef,它相当于给定的匿名结构。

typedef tagMyStruct struct { ... } MYSTRUCT;

定义了一个名为 MYSTRUCT 的 typedef 和一个名为 tagMyStruct 的类型。所以 MYSTRUCT 只是 tagMyStruct 的 typedef。因此,MYSTRUCT pStruct 定义了一个名为pStructtagMyStruct

您给出的赋值无效,因为 new MYSTRUCT 返回一个指向 MYSTRUCT指针

struct tagMyStruct { ... };

defines a new C++ type (class) called tagMyStruct.

struct { ... } MYSTRUCT;

defines a variable called MYSTRUCT with the given structure.

typedef struct { ... } MYSTRUCT;

defines a typedef called MYSTRUCT which is equivalent to the given anonymous struct.

typedef tagMyStruct struct { ... } MYSTRUCT;

defines a typedef called MYSTRUCT and a type called tagMyStruct. So MYSTRUCT is just a typedef for tagMyStruct. Therefore, MYSTRUCT pStruct defines a tagMyStruct called pStruct.

The assignment you gave is invalid, since new MYSTRUCT returns a pointer to MYSTRUCT.

笑红尘 2024-10-12 19:19:51

你错了,你使用的是typedef,即MYSTRUCTtagMyStruct的别名。这解释了c++是如何正确的。

为了创建变量,删除 typedef:

struct tagMyStruct {
    int numberOne;
    int numberTwo;
} MYSTRUCT;

You are wrong, you are using typedef, i.e. MYSTRUCT is an alias for tagMyStruct. This explains how it's correct c++.

In order to create a variable, drop the typedef:

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