Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!eecae!netnews.upenn.edu!rutgers!att!ihlpb!nevin1 From: nevin1@ihlpb.ATT.COM (Liber) Newsgroups: comp.lang.c++ Subject: Re: unions and classes with constructors Keywords: union, constructor Message-ID: <9595@ihlpb.ATT.COM> Date: 15 Feb 89 20:52:15 GMT References: <11560@watdragon.waterloo.edu> Reply-To: nevin1@ihlpb.UUCP (55528-Liber,N.J.) Distribution: comp Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 47 In article <11560@watdragon.waterloo.edu> akwright@watdragon.waterloo.edu (Andrew K. Wright) writes: >I would like to take issue with the rule that classes with constructors >with constructors may not appear as elements of a union. >So long as the user >calls the constructor explicitly for the field (s)he wishes to use, >no problem occurs. This is analogous to remembering to intialize >locals before you use them. You must initialize a union field before >you use it. But this blows my abstract notion of what a class is. To me, a class is, among other things, an interface. A user of a class should not have to worry about the details of the implementation of a class. Calling a constructor explicitly requires me, from the user's point of view, to understand something about how classes are implemented. You should not have to go beneath the abstract level of "new" and "delete" when using a class. [Note: by user, I don't include inheritance of one class into another class.] >Thus the restriction that C++ imposes seems unnecessary >here, IF your class has no destructor and does not take over storage allocation. This is where the real problem is. This not only blows the abstraction, but classes could no longer be clean interfaces. The user of a class should not be dependent on the implementation of a class. With your proposal, the use of the class in a union would be entirely dependent on whether or not I have a destructor for that class. The usage of the class would always be tied to the implementation; why have a class at all? >eg. > class Complex { > double re, im; > Complex( double r, double i ) { re = r; im = i; } > } >With the above simple class, there seems no reason not to allow it >inside a union. I would not be as free to reimplement Complex in any way that I choose. One of the advantages of C++ is that I can reimplement a class and not have to touch any of the code using the class; your proposal would violate this. -- _ __ NEVIN ":-)" LIBER nevin1@ihlpb.ATT.COM (312) 979-4751 IH 4F-410 ' ) ) "I will not be pushed, filed, stamped, indexed, / / _ , __o ____ briefed, debriefed or numbered! My life is my own!" / (_