Thread: Vector class

  1. #1
    Registered User
    Join Date
    May 2006

    Vector class

    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:
    #include <vector>
    template < class T >
    class Vector
    	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;
    #endif // VECTOR_H_INCLUDED
    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 ?

    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&#37; sure.

    Thanks a lot.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    You have a choice in the second option - do you want to return degrees or radians? Either way I'd return a double of some description - I never liked the float type much. Make a method for each option. You might also want to consider returning a COORD, x and y values. There's no point in just working in polar coordinates. The other one, that I can say but not spell, is useful too. For the former question ... could be difficult to have the number of dimensions as a variable. Stick with fixing a static number of dimensions first.

  3. #3
    Registered User
    Join Date
    Jan 2005
    In general, an std::vector is as efficient as any other similar dynamic memory solution. Since the size of your Vector will be dynamic, you will probably want to use dynamic memory. So, I would use std::vector. If you want to cap the size of your Vector at something small like 10, then a local array of that maximum might be an option to avoid dynamic allocation.

Popular pages Recent additions subscribe to a feed