Union of classes within a class
Im tyring to create a union of classes nested within a "parent" class C++ analogous to a union of structures within a "parent" data structure in C. For example,
struct Parent{
int struct_id;
union Children{
StructA structureA;
StructB structureB;
....
}
}
Transforming into something like:
class Parent{
int class_id;
union Children{
ClassA classA;
ClassB classB;
....
}
}
My question is for the class of unions, what is the most efficient way to implement this kind of class. Is there an existing implementation which allows you to determine or identify which class type resides in the parent class?
Thanks a Million,
EVAC
[783 byte] By [
evac-q8r] at [2007-11-11 6:33:06]

# 1 Re: Union of classes within a class
I doubt that this C technique is useful in C++. Remember that members of a union can't have constrcutors, destructors, virtual member functions and base classes, which narrows your choice to C structs. If you want to store two POD structs in a union, that's fine but in C++ you have better design choices such as deriving classes and using polymorphism.
The real question is what your goal is. If you want to save memory, then such a union isn't worth the trouble because the space saved by packing two structs will be wasted on a type field that specifies which struct the union currently holds. If you want to autmate type casting between the two structs then a conversion operator can do the trick.
Danny at 2007-11-11 21:03:07 >

# 2 Re: Union of classes within a class
you could try
// make an identifiable base class
struct AB_Base{
const int id;
AB_Base(int typeID) : id(typeID){}
}
// make sure ClassA ( and ClassB ) inherit the identifiable
class ClassA: public AB_Base
{
//... your class stuff
}
class Parent{
union Children{
ClassA classA;
ClassB classB;
....
}
}
/* now Parent::classA.id overlaps with Parent::classA.id. Both of them tell you the id of the object, irrespective of which it is*/
# 3 Re: Union of classes within a class
This code won't compile. Your classes have a constructor so you can't make them members of a union. The problem is that you can't give up theconstructor because you have a const data member. This is why unions in C++ are far less useful than they are in C.
Danny at 2007-11-11 21:05:06 >

# 4 Re: Union of classes within a class
OK I'm somewhat new at C++. My goal is to create a list of objects presumably using a container (vector,list,queue,etc) in which the objects are not necessarily instances of the same class or definition. Would you still suggest that I use a conversion operator for automated type casting? I understand how to this is done using union structs in C, but in C++ I am unsure as to whether or not I should use a base class with many derived classes or if I should use completely independent classes along with templates or what.? I would greatly appreciate if anyone can just point me in the right direction.
EVAC
# 5 Re: Union of classes within a class
OK, so you're looking for a heterogeneous container. Unions are certainly not an ideal choice for this task. Instead, try to think of another idea: stopring pointers to polymorphic objects in a container. Because all pointers occupy the same size, the container can accommodate different pointer types. Using virtual functions, it's possible to do something like:
vector<Base*> myvector;
myector[0]->doSomething();
where doSomething is a virtual function defined in the base class and overridden in every derived class.
BTW, this is a good question becuase it teaches an important lesson: always start with the design goal (i.e., a heterogeneous container) and then explore all the implementation options at your disposal, rather than sticking to a specific implementation that isn't necessarily ideal or even feasible.
Danny at 2007-11-11 21:07:06 >
