Original Post
here is a lil' slick trick brought together from discussions about pointer to data members taken to the almost extreme and cost you almost nout: [Edited by - snk_kid on October 29, 2004 5:52:58 PM]
#include <cstddef>
#include <iostream>
template< typename T >
struct Vector4 {
typedef size_t size_type;
private:
typedef T Vector4<T>::* const vec[4];
static const vec v;
public:
T x, y, z, w;
Vector4(T _x = 0, T _y = 0, T _z = 0, T _w = 0):
x(_x), y(_y), z(_z), w(_w) {}
const T& operator[](size_type i) const {
return this->*v;
}
T& operator[](size_type i) {
return this->*v;
}
};
template< typename T >
const typename Vector4<T>::vec Vector4<T>::v = { &Vector4<T>::x, &Vector4<T>::y, &Vector4<T>::z, &Vector4<T>::z };
template< typename T >
struct matrix4 {
typedef size_t size_type;
private:
typedef Vector4<T> matrix4::* const mat[4];
static const mat a;
public:
Vector4<T> i, j, k, l;
matrix4(const Vector4<T>& _i = Vector4<T>(),
const Vector4<T>& _j = Vector4<T>(),
const Vector4<T>& _k = Vector4<T>(),
const Vector4<T>& _l = Vector4<T>())
: i(_i), j(_j), k(_k), l(_l) {}
const Vector4<T>& operator[](size_type i) const {
return this->*a;
}
Vector4<T>& operator[](size_type i) {
return this->*a;
}
};
template< typename T >
const typename matrix4<T>::mat matrix4<T>::a = { &matrix4<T>::i, &matrix4<T>::j, &matrix4<T>::k, &matrix4<T>::l };
int main() {
matrix4<int> m;
m[0][0] = 10;
std::cout << "m[0][0]: " << m[0][0] << '\n';
std::cout << "m[0].x: " << m[0].x << '\n';
std::cout << "m.i.x: " << m.i.x << '\n';
return 0;
}