如何使用嵌套在无名称的struct中的c联合 [英] How to use c union nested in struct with no name
问题描述
我正在研究所谓的Hotspot开源项目,并查看实现,发现结构中有一个讨厌的嵌套联合,看起来像这样:
I'm working on the so called Hotspot open source project, and looking at the implementation I found a nasty nested union in struct looking like that:
typedef struct RC_model_t_st
{
union
{
struct block_model_t_st *block;
struct grid_model_t_st *grid;
};
/* block model or grid model */
int type;
thermal_config_t *config;
}RC_model_t;
据我所知,在C / C ++中,联合是不可接受的。那么,某人如何利用以这种方式声明的并用于什么目的呢?
As far as I'm aware in C/C++ that union is unaccesible. So how someone can make use of union declared in such manner and for what purpose?
谢谢!
推荐答案
这是一个匿名联合。在C ++中,根据[class.union]第5段:
This is an anonymous union. In C++, as per [class.union], paragraph 5:
出于名称查找的目的,在匿名联合定义之后,
被视为已在匿名联合定义的范围内定义了
。已声明
For the purpose of name lookup, after the anonymous union definition, the members of the anonymous union are considered to have been defined in the scope in which the anonymous union is declared
这意味着您可以访问其成员,就像它们是 RC_model_t_st
。
This means you can access its members as if they were members of RC_model_t_st
.
这篇关于如何使用嵌套在无名称的struct中的c联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!