Topic: Union of Classes?


Author: cimshop!davidm@uunet.UU.NET (David S. Masterson)
Date: 28 Sep 91 01:58:07 GMT
Raw View
Just an off-the-wall question:

Is there such a thing as a union of user-defined classes?  How does
constructors and destructors work in the face of the union?  Anybody got a
possible use for this?
--
====================================================================
David Masterson     Consilium, Inc.
(415) 691-6311     640 Clyde Ct.
uunet!cimshop!davidm    Mtn. View, CA  94043
====================================================================
"If someone thinks they know what I said, then I didn't say it!"




Author: schierz@iobj.UUCP (Ruediger Schierz)
Date: 30 Sep 91 18:45:29 GMT
Raw View
In article <CIMSHOP!DAVIDM.91Sep27185807@uunet.UU.NET> David S. Masterson
writes:

>Is there such a thing as a union of user-defined classes?  How does
>constructors and destructors work in the face of the union?  Anybody got a
>possible use for this?

One advantage of data abstraction is that the object itself knows of what type
it is and how to response to a message (key mechanismn of OO). If you use
unions, you will loose this advantage or at least you will have to do extra
code to determine the object types (see Lippman's C++ Primer p. 226 for an
example how to avoid accidentally retrieving an union value through an
inappropriate data member).
While an union is not a class it has neither a constructor nor a destructor.
To initialize unions you had to use the operator= (may be user defined).
Class A;
Class B;
union AB {
    A a;
    B b;
};

AB x;
x.a = A(); // Constructor of A

If you need deinitialization you had to code a pseudo-destructor-memberfunction
like: suizid() and call it after retrieving the union value.
x.a.suizid();

BTW: Isn't it an abstract base class what you want? Something like:

class AB;

class A :public AB {...};
class B :public AB {...};

AB a = new A;
AB b = new B;


--
Rudiger Schierz
   ____
__/ iO \_________________________________Interactive Objects
  \____/                                         Germany

Email: schierz@iobj.uucp
Phone: (+49)-7682-6374




Author: jimad@microsoft.com (Jim ADCOCK)
Date: 30 Sep 91 23:51:37 GMT
Raw View
In article <CIMSHOP!DAVIDM.91Sep27185807@uunet.UU.NET> cimshop!davidm@uunet.UU.NET (David S. Masterson) writes:
|Just an off-the-wall question:
|
|Is there such a thing as a union of user-defined classes?  How does
|constructors and destructors work in the face of the union?  Anybody got a
|possible use for this?

See ARM page 181 [section 9.5]

A union can't have most of what we think of as relating to classes.
No parents, no childen, no members with constructors, destructors, nor
user-defined assignment operators [sounds like a definition-error
to me, seems like any member-wise assignment is enough to break unions]

Unions can have constructors, destructors, and member functions, but
not virtual.

In OOP terms I think what you want instead is a pointer to a a heirarchy of
polymorphic types with virtual functions.  The "contents" of the
pointer then are whatever object is presently pointed-at.  Using
this approach, one is not constrained to have all the polymorphic objects
fit in the same space.  You *could* make your own "Union" class if
you insisted out of such a pointer plus "a bunch of bytes" in which
you create one or the other type object in using overloaded operator new.
But why bother?  The result would be more complicated and not terribly
different from typical polymorphic "OO" programming in C++.  Compare
the difference between a polymorphic "Collection" of objects, verses
a single "Union" of objects, for instance.  Conceptually, not much
difference.  [Might some day be a useful hack to avoid using GC though :-]