[comp.lang.c++] How to correctly initialize struct/class

poffen@sj.ate.slb.com (Russ Poffenberger) (08/21/90)

Hi,

Is there a way (simple) to do the following.

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?

Russ Poffenberger               DOMAIN: poffen@sj.ate.slb.com
Schlumberger Technologies       UUCP:   {uunet,decwrl,amdahl}!sjsca4!poffen
1601 Technology Drive		CIS:	72401,276
San Jose, Ca. 95110             (408)437-5254

steve@taumet.com (Stephen Clamage) (08/21/90)

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

poffen@sj.ate.slb.com (Russ Poffenberger) (08/22/90)

In article <406@taumet.com> steve@taumet.com (Stephen Clamage) writes:
>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


When I try this with g++ (version 1.37.1) I get the following errors..

test_union4.cc: In method data::data (class ugly, class ugly, class ugly, classugly, class ugly):
test_union4.cc:26: too few arguments for constructor `ugly'
test_union4.cc:26: in base initialization for class `ugly'
test_union4.cc:26: too few arguments for constructor `ugly'
test_union4.cc:26: in base initialization for class `ugly'
test_union4.cc:26: too few arguments for constructor `ugly'
test_union4.cc:26: in base initialization for class `ugly'
test_union4.cc:26: too few arguments for constructor `ugly'
test_union4.cc:26: in base initialization for class `ugly'
test_union4.cc:26: too few arguments for constructor `ugly'
test_union4.cc:26: in base initialization for class `ugly'

I will try it tonight on TC++ 1.0 and see what happens.


Russ Poffenberger               DOMAIN: poffen@sj.ate.slb.com
Schlumberger Technologies       UUCP:   {uunet,decwrl,amdahl}!sjsca4!poffen
1601 Technology Drive		CIS:	72401,276
San Jose, Ca. 95110             (408)437-5254