Hey there. I'm pursuing my Matrix class and would like to make it work together with a Vector class I'm developing as well. I have a design question however and a technical question as well.
First, here's the code for the Vector class right now:It isn't as complete as it should, but this is a good start for a beginner's Vector class. I want to be able to use that Vector class with matrices; therefore I need it to let the user use a 2, 3, 5 or even 10 dimension vector. I first thought about using std::auto_ptr< > and allocate enough memory for each dimension, but copying objects would be a mess because of pointer ownership. Then I thought of using a vector, but it seems like overhead to me since vectors don't usually get large enough for vectors to be efficient. I thought then to use a pointer and allocate memory dynamically but this seems a bit hack-ish to me. Do you guys know a good way to solve that problem ?Code:#ifndef VECTOR_H_INCLUDED #define VECTOR_H_INCLUDED #include <vector> template < class T > class Vector { public: Vector(int n = 3); Vector(const Vector< T >&); // Not quite sure about the type it should return... float Angle( ) const; void Normalize( ); float Length( ) const; Vector< T > VecProduct(const Vector< T >&) const; T DotProduct(const Vector< T >&) const; bool IsParallel(const Vector< T >&) const; bool IsOrthogonal(const Vector< T >&) const; bool operator == (const Vector< T >&) const; bool operator != (const Vector< T >&) const; Vector< T >& operator = (const Vector< T >&); Vector< T >& operator += (const Vector< T >&); Vector< T >& operator + (const Vector< T >&) const; Vector< T >& operator -= (const Vector< T >&); Vector< T >& operator - (const Vector< T >&) const; Vector< T >& operator *= (const T&); Vector< T >& operator * (const T&) const; private: }; #endif // VECTOR_H_INCLUDED
Also, the technical question is rather simple, in fact. What type should the Angle() function return ? I thought returning a T would be dangerous because if T happens to be 'int' then we lose precision (which is also a problem for Normalize() and Length()). Then I thought returning float would be correct because of its precision but I'm not 100% sure.
Thanks a lot.



LinkBack URL
About LinkBacks


