这两种带有结构的 typedef 实现之间有什么区别(如果有的话)?
typedef struct Data{
int x;
int y;
} Data;
和
struct Data{
int x;
int y;
};
typedef struct Data Data;
最佳答案
是的,它们都 typedef Data
为 struct Data
,尽管人们通常会使用不同的名称以避免混淆。
请注意,在 C++ 中,使用类型时根本不需要 struct
部分,因此通常根本没有 typedef
。这同样适用于enum
等。
struct Data
{
int x;
int y;
};
void foo1(Data data); // OK in C++
void foo2(struct Data data); // OK in C or C++, but almost never seen in C++
typedef struct Data2_
{
int x;
int y;
}Data2;
void foo3(Data2 data); // OK in C or C++, but C++ code tends to skip the typedef as well
关于c - 这两种具有结构的 typedef 实现是否等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46057500/