Wanted: A C++ Trick...

Itay 'z9u2K' Duvdevani z9u2k@bezeqint.net
Sat Jan 4 16:09:00 GMT 2003


Hi all,
I'm programming my brand new math library and I'm having some problems 
with my Vector/Matrix relationship...

I need two representations of a vector and three of a matrix.

Vector's representations:
* 3 components (xyz)
* floating point vector

Matrix's representations:
* 16 floats
* four 3 components vectors and four floats
* 16 floats vector

The simplest thing to do:
class Vector
{
public:
    union
    {
        struct
        {
            float x, y,z;
        };
        float Vec[3];
    };

    // stuff
};

class Matrix
{
public:
    union
    {
        struct
        {
            float _11,  _12 ..., _44;
        };

        struct
        {
            Vector v1; float f14;
            Vector v2; float f24;
            Vector v3; float f34;
            Vector v4; float f44;
        };
        float Mat[16];
    };
};

that should do it.

but I doesn't.

I'm using RH 8.0 & gcc 3.2

When I comple the above code, g++ warns me ISO C++ forbids unnamed 
stucts or something like that...

so I did this:
class Vector
{
    struct VectorComps
    {
        float x, y, z;
    };

public:
    union
    {
        VectorComps vec;
        float Vec[3];
    };
    float &x, &y, &z;

    Vector() : x(vec.x), y(vec.y), z(vec.z) {}

    // stuff
};

Now vector compiles without warnnings, but I cannot put Vector into the 
Matrix's union since it:
a. has a constructor
b. does not have the size of three floats anymore and will not align 
right into matrix's memory...

PS
I've tried to create a base class for vector that only has three floats 
and drive Vector from it, it worked ok, but now the vector's matrix has 
does not have access to the "float Vec[3]" representations and does not 
have any of the operators defined for Vector... not good... Matrix must 
have acces to the "float Vec[3]" representation, the "float x, y, z" 
representation and to all of the operators...

I need a "trick" that can overcome this... thanks...



More information about the Gcc-help mailing list