Union of classes within a class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Union of classes within a class

  1. #1
    Join Date
    Dec 2003
    Posts
    38

    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,

    Code:
    struct Parent{
      int struct_id;
      union Children{
         StructA   structureA;
         StructB   structureB;
         .....
      }
    }
    Transforming into something like:

    Code:
    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
    Last edited by evac-q8r; 08-03-2005 at 08:12 PM.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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 Kalev

  3. #3
    Join Date
    Aug 2005
    Location
    Jamaica, W.I.
    Posts
    4
    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*/

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    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 Kalev

  5. #5
    Join Date
    Dec 2003
    Posts
    38
    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

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    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:
    Code:
    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.
    Last edited by Danny; 08-04-2005 at 10:01 PM.
    Danny Kalev

Similar Threads

  1. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  2. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  3. Collection classes revisited
    By Marco Antonio Pérez López in forum .NET
    Replies: 0
    Last Post: 06-09-2001, 10:22 PM
  4. Collection Classes revisited
    By Marco Antonio Pérez López in forum .NET
    Replies: 0
    Last Post: 06-09-2001, 10:02 PM
  5. Get 10 months forward with select statment
    By Nirit Touboul in forum Database
    Replies: 7
    Last Post: 02-25-2001, 11:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center