struct ComplexType { enum DataType { Int, Char } Type; // ... };
Type
是结构体ComplexType
的成员变量,它的类型是枚举类型DataType
,而这个枚举类型是嵌套在结构体ComplexType
内的,因此需要用ComplexType::Int
来表示枚举常量Int
。
如果枚举类型不是嵌套在结构体内,参考以下代码
// 枚举类型的定义 enum DataType { Int, Char } type; // 变量type的类型是enum DataType // 以下是对type的操作 type = Int; // 将枚举值Int赋值给变量type DataType type2 = Char; // 将枚举值Char赋值给变量type
回到楼主的代码上
myData1.Type = ComplexType::Int;
Type
变量是定义在结构体内的成员变量,myData1
是一个结构体变量,因此用myData1.Type
表示这个结构体变量的Type
成员;Int
是枚举类型DataType1
的一个枚举值,而该枚举类型是嵌套在结构体ComplexType
内的,所以枚举值也是嵌套在结构体ComplexType
内,因此用ComplexType::Int
表示这个枚举值。