Original Post
Hey all. I've made some pretty simple vector template classes for 2, 3 and 4 elements, with overloaded operators and a couple of extra methods. Then with a few typedef calls I've got int2, float2, int3, etc classes defined. The problem comes when trying to for eg. assign a float2 to an int2. I want to be able to overload the copy constructor for specific types (T) as well as others but I think i've reached a limitiation of templates... For addition and subtraction of vector2s of different types I can globally overload the (+|-) operators, but I can't globally overload the (=) operator. Template specialisations could be made so I could overload the (=) operator inside the class but it would defeat the purpose of using templates, because I'd have to write ALL of the member functions into the specialised template (as far as I know). Does anyone know a way around this? Thanks, Brendan.
template <class T>
class vector2 {
public:
vector2() { x=0; y=0; }
vector2(const T &nx, const T &ny) { x=nx; y=ny; }
vector2<T> &operator=(const vector2<T> &n) { x=n.x; y=n.y; return *this; }
// ...
T x,y;
};
typedef vector2<int> int2;
typedef vector2<float> float2;