如何在 C++ 中为嵌套结构创建别名?
How to create an alias for a nested structure in C++?
我无法在 C++ 中为嵌套结构创建别名。假设我们有下面这段代码。现在,如何使用 typedef
等为 struct Score
创建别名?
struct Information
{
struct Score
{
struct Score *link;
float src;
} *src;
};
如果 struct Score
仅在 struct Information
内声明,您将无法将其用作自己的实体。所以打破它。
struct Score {
struct Score *link;
float src;
};
struct Information {
struct Score *src;
};
在struct Information
声明后使用typedef
即可。没有理由在结构 Information
.
中定义新类型
struct Information
{
struct Score
{
struct Score *link;
float src;
} *src;
};
typedef struct Score score;
我无法在 C++ 中为嵌套结构创建别名。假设我们有下面这段代码。现在,如何使用 typedef
等为 struct Score
创建别名?
struct Information
{
struct Score
{
struct Score *link;
float src;
} *src;
};
如果 struct Score
仅在 struct Information
内声明,您将无法将其用作自己的实体。所以打破它。
struct Score {
struct Score *link;
float src;
};
struct Information {
struct Score *src;
};
在struct Information
声明后使用typedef
即可。没有理由在结构 Information
.
struct Information
{
struct Score
{
struct Score *link;
float src;
} *src;
};
typedef struct Score score;