There's a curious thing you can do with C++ (You didn't specify a language, and this answer is mostly because I think it's nice to see alternatives, though I don't really believe this is useful in most cases.)
Using templates you can do something like this:
template <class T, class S, int U>
class VectorN
{
protected:
int _vec[U];
public:
S& operator+=(const S c)
{
for(int i = 0; i < U; i++)
{
_vec[i] += c.at(i);
}
return (S&)*this;
}
int at(int n) const
{
return _vec[n];
}
};
template <class T>
class Vec2 : public VectorN<T,Vec2<T>,2>
{
public:
T& x;
T& y;
Vec2(T a, T b) : x(this->_vec[0]), y(this->_vec[1])
{
this->_vec[0] = a;
this->_vec[1] = b;
}
};
template <class T>
class Vec3 : public VectorN<T,Vec3<T>,3>
{
public:
T& x;
T& y;
T& z;
Vec3(T a, T b, T c) : x(this->_vec[0]), y(this->_vec[1]), z(this->_vec[2])
{
this->_vec[0] = a;
this->_vec[1] = b;
this->_vec[2] = c;
}
};
and this can be used like this:
int main(int argc, char* argv[])
{
Vec2<int> v1(5,0);
Vec2<int> v2(10,1);
std::cout<<((v1+=v2)+=v2).x;
return 0;
}
Like I said, I don't think this is useful, and it will probably complicate your life when you try to implement dot/normalize/other stuff and try to be generic with any number of vectors.
Vector3
should be just 3floats
as far as the memory is concerned. Not saying that's impossible, just that I've never seen that in a production engine. \$\endgroup\$floats
. You know, YAGNI, KISS, all that stuff.Vector2
,Vector3
andVector4
with no inheritance andfloats
only is really the de facto standard in game engines. \$\endgroup\$typedef float real;
;). \$\endgroup\$