13.1.8 Unions

A union in OMG IDL is mapped to a C++ class. The C++ class contains the following:

  • Constructors
  • Destructors
  • Assignment operators
  • Modifiers for the union value
  • Accessors for the union value
  • Modifiers and accessors for the union discriminator

For example, consider the following OMG IDL definition:

// OMG IDL
union OrderItem switch (long)
    {
    case 1: itemStruct itemInfo;
    case 2: orderStruct orderInfo;
    default: ID idInfo;
    };

This definition maps to C++ as follows:

// C++
class OrderItem
    {
    public:
       OrderItem();
       OrderItem(const OrderItem &);
       ~OrderItem();

        OrderItem &operator=(const OrderItem&);
      void _d (CORBA::Long);
      CORBA::Long _d () const;

      void itemInfo (const itemStruct &);
      const itemStruct & itemInfo () const;
      itemStruct & itemInfo ();

      void orderInfo (const orderStruct &);
      const orderStruct & orderInfo () const;
      orderStruct & orderInfo ();

      void idInfo (ID);
      ID idInfo () const;

      . . .
};

The default union constructor does not set a default discriminator value for the union; therefore, you cannot call any union accessor member function until you have set the value of the union. The discriminator is an attribute that is mapped through the _d member function.