public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Wanted: A C++ Trick...
@ 2003-01-04 16:09 Itay 'z9u2K' Duvdevani
  2003-01-07 13:10 ` John Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Itay 'z9u2K' Duvdevani @ 2003-01-04 16:09 UTC (permalink / raw)
  To: gcc-help

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...

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Wanted: A C++ Trick...
  2003-01-04 16:09 Wanted: A C++ Trick Itay 'z9u2K' Duvdevani
@ 2003-01-07 13:10 ` John Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2003-01-07 13:10 UTC (permalink / raw)
  To: Itay 'z9u2K' Duvdevani, gcc-help

Hi Itay,

I recommend hiding the implementation entirely, within the class.  Don't use
the union, it's more trouble than it's worth.

I'm just guessing as to what kind of accessors you need.  I'll leave the
constructors to your own devising.

class Vector
{
public:
  float& X() { return v[0]; }
  float& Y() { return v[1]; }
  float& Z() { return v[2]; }
  float* array() { return v; }
  float& operator [] (int index) { return v[index]; }

private:
  float v[3];
};

class Matrix
{
public:
  // Whatever.

  // SPOOF a vector (because we "know" that layout is right).
  Vector& GetVector(int index) { return *(Vector*)&v[index]; }
private:
  float v[4][4];
};

The point is you want to HIDE your data, not expose it.  Hiding is a key
encapsulation facility of C++.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-01-07 13:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-04 16:09 Wanted: A C++ Trick Itay 'z9u2K' Duvdevani
2003-01-07 13:10 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).