I have a structure of below type
struct enumStruct
{
enum Number
{
zero = 0,
one = 1
};
Number t;
union
{
/* t = 0 */
some_struct_zero zero;
/* t = 1 */
some_struct_one one;
} un;
} v_enumStruct;
Can I assign a value to v_enumStruct.t
as below?
v_enumStruct.t = v_enumStruct.zero;//Option 1
Also, the below does not seem to work
v_enumStruct.t = zero; //Option 2 - zero is mistaken as an unavailable variable name
1) zero
is not a member of struct
. 2) The enum
inside struct
doesn't declare any member. It can be declared anywhere. enum members are named constants, they are not variables and 3) Number
needs the enum
qualifier:
struct enumStruct
{
enum Number
{
zero = 0,
one = 1
};
enum Number t;
union
{
/* t = 0 */
some_struct_zero zero;
/* t = 1 */
some_struct_one one;
} un;
} v_enumStruct;
You might as well move the enum Number { ..};
outside the struct
as it's not doing anything inside the struct. Now you should be able to assign:
v_enumStruct.t = zero;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.