Jump to content
  • Advertisement
Sign in to follow this  

Imposing a condition on the vector addition of two different vector types

This topic is 3581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, I've written a Vector3 classe...It's a class template and here is a forward declaration:
template<typename Real,class MathLib> class Vector3;




Real is of course a built-in float type and MathLib is a class with static functions and variables only( policy class? ) that implements some algorithms. I initialy overloaded the most common operators that way:
template<typename Real,class MathLib>
class Vector3
{
    public:    
        ...
        const Vector3 operator+( const Vector3<Real,MathLib>& )const;
    
    private:
        Real x[3];
};




This is actually a limited design since the operator ares only defined for class templates with the same ( template ) parameters...A more general design would be:
template<typename Real,class MathLib>
class Vector3
{
    public:
        ...
        template<class Vector>
        const Vector3 operator+( const Vector& v )const
        {
            return Vector3( x[0] + v[0], x[1] + v[1], x[2] + v[2] );
        }         
};





That's more general except that I want to be able to impose properties on the template parameter( Vector and Matrix ), like dimensions. Without them I can add a Vector3 and a Vector4, since the latter has at least three elements. The first idea I had is to define a static variable and to test inside the "+" operator of template<typename Real,class Math> Vector3, if the dimensions of the vectors are equal.
templaye<typename Real,class Math>
class Vector3
{
    public:
           const static int DIMENSION;
           ...
};

template<typename Real,class Math>
template<Vector>
const Vector3<Real,Math> Vector3<Real,Math>::operator+( const Vector& v )const
{
    assert( Vector::DIMENSION == Vector3::DIMENSION )

    return Vector3<Real,Math>( x[0] + v[0], x[1] + v[1], x[2] + v[2] );
}

template<typename Real,class Math> int const Vector3<Real,Math>::DIMENSION = 3;





Is a there another( more elegant ) way to solve the problem?

Share this post


Link to post
Share on other sites
Advertisement
Of the many, many, many (frankly, way too many) game math libraries I've seen, the best ones use a fair amount of copy-paste reuse. Remember, elegance in an implementation is important, but elegance in the interface is infinitely more so. Moreover, the more you play with Vector2, Vector3, and Vector4, the less alike they will look. Just make them different classes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Of the many, many, many (frankly, way too many) game math libraries I've seen, the best ones use a fair amount of copy-paste reuse. Remember, elegance in an implementation is important, but elegance in the interface is infinitely more so. Moreover, the more you play with Vector2, Vector3, and Vector4, the less alike they will look. Just make them different classes.


They are...and with the way I've implemented my Vector3 class, I can have as many Vector3 types as policy classes. I simply want to avoid adding 2 vectors of different lengths, in my template overloaded operator+. I want to be able to add two Vector of the same length but of different types, elegantly. The idea I had came naturally but I just want to know if there is a more elegant way to achieve the goal...

Share this post


Link to post
Share on other sites
Then simply parameterize on the parameters of the Vector3 template, rather than on the vector type as a whole.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!