Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: How to correctly initialize struct/class Message-ID: <406@taumet.com> Date: 21 Aug 90 15:44:53 GMT References: <1990Aug20.220157.24736@sj.ate.slb.com> Organization: Taumetric Corporation, San Diego Lines: 74 poffen@sj.ate.slb.com (Russ Poffenberger) writes: |I want to define a union, such that it can be either a float value, or a |pointer. | |union both { | float value; | float *value; |}; | |Now I want to define a structure (or class) like so that uses several instances |of this union. | |struct data { | union both one; | union both two; | union both three; | union both four; | union both five; |}; | |Now what I need to do is to declare an instance of the struct, but initialize |all of the fields. Any of the fields may be initialized to either a floating |point constant, or the address of a floating variable. | |e.g. | |float f1; |float f2; | |struct data d1 = (&f1,4.3,&f2,7.2,0.0) |struct data d2 = (0.0,1.0,2.0,3.0,4.0) | |etc... | |How can I declare a constructor to do this? Is it even possible? Try this: class ugly { union { float f; float* p; }; public: ugly(float x) { f = x; } ugly(float* x) { p = x; } }; class data { ugly u1; ugly u2; ugly u3; ugly u4; ugly u5; public: data(ugly a1, ugly a2, ugly a3, ugly a4, ugly a5) { u1 = a1; u2 = a2; u3 = a3; u4 = a4; u5 = a5; } }; Now you can use float f1; float f2; data d1(&f1, 4.3, &f2, 7.2, 0.0); data d2(0.0, 1.0, 2.0, 3.0, 4.0); -- Steve Clamage, TauMetric Corp, steve@taumet.com