views:

148

answers:

6

I need to have several instances of a union as class variables, so how can I create a union instance in the heap? thank you

A: 

Use the new operator.

Parappa
A: 

Same as a struct :) You can use malloc() and do it the C way, or new for the C++ way. The secret is that structs, unions and classes are related; a struct is just a class (usually) without methods. There's more clarification in the following comments, should you care.

Carl Smotricz
A struct in C++ may have methods. A struct and class are very similar except that a struct has public members/functions by default while a class has private members by default.
RC
No, you can have methods in structs. You can even derive structs from classes and vice-versa. About the only difference between struct and class is that the default access for a struct is public, while it's private for a class.
Fred Larson
In C++, a struct can have methods. The main difference between a struct and a class is that the default access for a class is private while the default access for a struct is public.
R Samuel Klatchko
Struct can have methods in C++, the main difference between class and struct is that the default scope for struct is public while its private for class.
JP
OK OK, you can stop bashing me now.
Carl Smotricz
Yeah, that was quite a dog pile. 8v)
Fred Larson
You might find this question interesting: http://stackoverflow.com/questions/654609/how-much-functionality-is-acceptable-for-a-c-struct
Mark Ransom
Cool, thanks! I'd consider it in rather poor taste to cross-purpose the two so I'd forgotten C++ lets you do it anyway. "C++: The fully automatic foot-targeting bazooka!" :)
Carl Smotricz
Methods in structs? Heck, with C++ you can even have methods in _unions_ !
MSalters
+2  A: 

My C++ is a bit rusty, but:

   my_union_type *my_union = new my_union_type;
   ...
   delete my_union;
Frank Schmitt
+5  A: 

The same as creating any other object:

union MyUnion
{
   unsigned char charValue[5];
   unsigned int  intValue;
};


MyUnion *myUnion = new MyUnion;

Your union is now on the heap. Note that a union is the size of it's largest data member.

RC
A: 

I'm not sure where you want to head with this. A union is a user-defined data or class type that, at any given time, contains only one object from its list of members. So starting from this, if you have a union defined like this:

union DataType
{
    char ch;
    integer i;
    float f;
    double d;
};

You can then use DataType as a type to define members in a class or as a type to define variables on the stack, just like regular types, struct or classes you define.

JP
A: 

Use the new operator:

#include <iostream>

union u {
  int a;
  char b;
  float c;
};

class c {
public:
  c() { u1 = new u; u2 = new u; u3 = new u; }
  ~c() { delete u1; delete u2; delete u3; }
  u *u1;
  u *u2;
  u *u3;
};

int main()
{
  c *p = new c;

  p->u1->a = 1;
  p->u2->b = '0' + 2;
  p->u3->c = 3.3;

  std::cout << p->u1->a << '\n'
            << p->u2->b << '\n'
            << p->u3->c << std::endl;

  delete c;

  return 0;
}
Greg Bacon