Purpose of Unions in C and C++

后端 未结 16 2196
予麋鹿
予麋鹿 2020-11-22 06:55

I have used unions earlier comfortably; today I was alarmed when I read this post and came to know that this code

union ARGB
{
    uint32_t colour;

    str         


        
16条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 07:34

    You could use unions to create structs like the following, which contains a field that tells us which component of the union is actually used:

    struct VAROBJECT
    {
        enum o_t { Int, Double, String } objectType;
    
        union
        {
            int intValue;
            double dblValue;
            char *strValue;
        } value;
    } object;
    

提交回复
热议问题