Wanted: A C++ Trick...

John Love-Jensen eljay@adobe.com
Tue Jan 7 13:10:00 GMT 2003

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
  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]; }

  float v[3];

class Matrix
  // Whatever.

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

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


More information about the Gcc-help mailing list