C: Where is union practically used?

2019-01-21 07:48发布

I have a example with me where in which the alignment of a type is guaranteed, union max_align . I am looking for a even simpler example in which union is used practically, to explain my friend.

标签: c++ c unions
18条回答
甜甜的少女心
2楼-- · 2019-01-21 08:24

I've used sometimes unions this way

//Define type of structure
typedef enum { ANALOG, BOOLEAN, UNKNOWN } typeValue_t;
//Define the union
typedef struct  {
  typeValue_t typeValue;
  /*On this structure you will access the correct type of
    data according to its type*/
  union {
    float ParamAnalog;
    char  ParamBool;
  };
} Value_t;

Then you could declare arrays of different kind of values, storing more or less efficiently the data, and make some "polimorph" operations like:

 void printValue ( Value_t value ) {
    switch (value.typeValue) {
       case BOOL:
          printf("Bolean: %c\n", value.ParamBool?'T':'F');
          break;
       case ANALOG:
          printf("Analog: %f\n", value.ParamAnalog);
          break;
       case UNKNOWN:
          printf("Error, value UNKNOWN\n");
          break;
    }
 }
查看更多
3楼-- · 2019-01-21 08:27

For convenience, I use unions to let me use the same class to store xyzw and rgba values

#ifndef VERTEX4DH
    #define VERTEX4DH

    struct Vertex4d{

        union {
            double x;
            double r;
        };
        union {
            double y;
            double g;
        };
        union {
            double z;
            double b;
        };
        union {
            double w;
            double a;
        };

        Vertex4d(double x=0, double y=0,double z=0,double w=0) : x(x), y(y),z(z),w(w){}
    };

#endif
查看更多
Emotional °昔
4楼-- · 2019-01-21 08:28

In the Windows world, unions are commonly used to implement tagged variants, which are (or were, before .NET?) one standard way of passing data between COM objects.

The idea is that a union type can provide a single natural interface for passing arbitrary data between two objects. Some COM object could pass you a variant (e.g. type VARIANT or _variant_t) which could contain either a double, float, int, or whatever.

If you have to deal with COM objects in Windows C++ code, you'll see variant types all over the place.

查看更多
三岁会撩人
5楼-- · 2019-01-21 08:29
struct cat_info
{
int legs;
int tailLen;
};

struct fish_info
{
bool hasSpikes;
};


union 
{
fish_info fish;
cat_info cat;
} animal_data;

struct animal
{
char* name;
int animal_type;
animal_data data;
};
查看更多
地球回转人心会变
6楼-- · 2019-01-21 08:30
  • When reading serialized data that needs to be coerced into specific types.
  • When returning semantic values from lex to yacc. (yylval)
  • When implementing a polymorphic type, especially one that reads a DSL or general language
  • When implementing a dispatcher that specifically calls functions intended to take different types.
查看更多
冷血范
7楼-- · 2019-01-21 08:30

Example:

When using different socket types, but you want a comon type to refer.

查看更多
登录 后发表回答