结构标记和名称,为什么局部变量声明为名称编译?

时间:2022-04-30 08:45:07

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.

根据我的理解,mytagstruct是新的数据类型MYSTRUCT是在这里创建的变量。

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?

它与Visual Studio 2010进行了良好的编辑。如何使用c++ ?我以为MYSTRUCT是变量而不是类型?

3 个解决方案

#1


9  

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是结构体的名称。在C语言中,与c++不同,每次使用struct类型时都必须显式地使用struct关键字。例如

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

为了避免一直写struct,将struct tagMyStruct定义为MYSTRUCT。现在你可以写

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

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

您认为这是(一个变量定义)没有typedef关键字,就像这样

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

是无效的C或c++。也许你的意思

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

hth

hth

#2


5  

struct tagMyStruct { ... };

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

定义一个新的c++类型(类),名为tagMyStruct。

struct { ... } MYSTRUCT;

defines a variable called MYSTRUCT with the given structure.

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

typedef struct { ... } MYSTRUCT;

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

定义一个名为MYSTRUCT的类型定义,它等价于给定的匿名结构。

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.

定义名为MYSTRUCT的类型定义和名为tagMyStruct的类型。MYSTRUCT是tagMyStruct的定义。因此,MYSTRUCT pStruct定义了一个名为pStruct的tagMyStruct。

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

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

#3


3  

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

您错了,您使用的是typedef,也就是说,MYSTRUCT是tagMyStruct的别名。这解释了它是如何正确使用c++的。

In order to create a variable, drop the typedef:

为了创建一个变量,删除typedef:

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

#1


9  

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是结构体的名称。在C语言中,与c++不同,每次使用struct类型时都必须显式地使用struct关键字。例如

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

为了避免一直写struct,将struct tagMyStruct定义为MYSTRUCT。现在你可以写

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

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

您认为这是(一个变量定义)没有typedef关键字,就像这样

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

是无效的C或c++。也许你的意思

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

hth

hth

#2


5  

struct tagMyStruct { ... };

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

定义一个新的c++类型(类),名为tagMyStruct。

struct { ... } MYSTRUCT;

defines a variable called MYSTRUCT with the given structure.

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

typedef struct { ... } MYSTRUCT;

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

定义一个名为MYSTRUCT的类型定义,它等价于给定的匿名结构。

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.

定义名为MYSTRUCT的类型定义和名为tagMyStruct的类型。MYSTRUCT是tagMyStruct的定义。因此,MYSTRUCT pStruct定义了一个名为pStruct的tagMyStruct。

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

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

#3


3  

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

您错了,您使用的是typedef,也就是说,MYSTRUCT是tagMyStruct的别名。这解释了它是如何正确使用c++的。

In order to create a variable, drop the typedef:

为了创建一个变量,删除typedef:

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