This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Wanted: A C++ Trick...


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]