Nested enumeration of structures to assign values to structure variables

Discuss in two cases
1. The enumeration type is defined outside the structure

//定义枚举类型
enum Flash
{
    
    
	AlwaysOn, //0开始递增
	AlwaysOff,
	AllFlashLight,
	AlternateFlash,
};

//定义结构体
typedef struct
{
    
    
	int FlashTime;
	enum Flash status;
}sLED;

int main()
{
    
    
	sLED LED;
	LED.FlashTime = 300;
	LED.status = AlwaysOn;
	return 0;
}

Enumeration type definitions can be used directly when they are external


2. The enumeration type is defined in the structure body

typedef struct
{
    
    
	int FlashTime;
	enum Flash
	{
    
    
		AlwaysOn, //0开始递增
		AlwaysOff,
		AllFlashLight,
		AlternateFlash,
	}status;
}sLED;

int main()
{
    
    
	sLED LED;
	LED.FlashTime = 300;
	LED.status = sLED::AlwaysOn;
	return 0;
}

Use the domain operator ::to indicate that AlwaysOn belongs to sLED, and it cannot be recognized directly

Guess you like

Origin blog.csdn.net/weixin_43689161/article/details/123665458