[Solved] C++ Class implementation [closed]


Although this is not a Matrix class but rather a simple Vector2 class that accepts floats this class demonstrates how you can overload the operators.

class Vector2 {     
public:
    union {
        float _f2[2];
        struct {
            float _x;
            float _y;
        };
    };

    inline Vector2();
    inline Vector2( float x, float y);
    inline Vector2( float *pfv );

    // Operators
    inline Vector2  operator+( const Vector2 &v2 ) const;
    inline Vector2  operator+() const;
    inline Vector2& operator+=( const Vector2 &v2 );
    inline Vector2  operator-( const Vector2 &v2 ) const;
    inline Vector2  operator-() const;
    inline Vector2& operator-=( const Vector2 &v2 );
    inline Vector2  operator*( const float &value ) const;
    inline Vector2& operator*=( const float &value );
    inline Vector2  operator/( const float &value ) const;
    inline Vector2& operator/=( const float &value );   

    inline friend Vector2 Vector2::operator*( const float &fValue, const Vector2 v2 ) {
        // Pre Multiple Vector By A Scalar
        return Vector2( value*v2._x, value*v2._y );
    } 

    inline friend Vector2 Vector2::operator/( const float &value, const Vector2 v2 ) {
        // Pre Divide Vector By A Scalar
        Vector2 vec2;
        // Math:: external class not shown here isZero() to check if value is minimally close to 0
        if ( Math::isZero( v2._x) )
            vec2._x= 0.0f;
        else
            vec2._x = value / v2._x;

        if ( Math::isZero( v2._y ) )
            vec2._y = 0.0f;
        else
            vec2._y = value / v2._y;

         return vec2;
    } 

};

inline Vector2::Vector2() :
  _x(0.0f), _y(0.0f) {  
} 

inline Vector2::Vector2( float x, float y ) :
  _x(x), _y(y) {
}

inline Vector2::Vector2( float *pv ) :
  _x( pv[0] ), _y( pv[1] ) {
}

inline Vector2 Vector2::operator+() const {
    return *this; // Unary + Operator
} 

inline Vector2 Vector2::operator+( const Vector2 &v2 ) const {
    // Binary + Take This Vector And Add Another Vector To It
    return Vector2( _x + v2._x, _y + v2._y );
}

inline Vector2 &Vector2::operator+=( const Vector2 &v2 ) {
    _x += v2._x;
    _y += v2._y;
    return *this;
}

inline Vector2 Vector2::operator-() const {
    return Vector2( -_x, -_y );  // Unary - Operator: Negate Each Value
}

inline Vector2 Vector2::operator-( const Vector2 &v2 ) const {
    // Unary - Take This Vector And Subtract Another Vector From It
    return Vector2( _x - v2._x, _y - v2._x );
}

inline Vector2 &Vector2::operator-=( const Vector2 &v2 ) {
    _x -= v2._x;
    _y -= v2._y;
    return *this;
}

inline Vector2 Vector2::operator*( const float &value ) const {
    // Post Multiply Vector By A Scalar
    return Vector2( _x*value, _y*value );
} 

inline Vector2& Vector2::operator*=( const float &value ) {
    // Multiply This Vector By A Scalar
    _x *= value;
    _x *= value;    
    return *this; 
}

inline Vector2 Vector2::operator/( const float &value ) const {
    // Post Divide Vector By A Scalar    
    Vector2 vec2;
    // Again Math:: not shown here...
    if ( Math::isZero( value ) ) {
        vec2._x = 0.0f;
        vec2._y = 0.0f;
    } else {
        float inv = 1/value;
        vec2._x *= inv;
        vec2._y *= inv;
    }    
    return vec2;  
}

inline Vector2& Vector2::operator/=( const float &value ) {
    // Divide This Vector By A Scalar Value
    // Same as above; Math:: not shown here.
    if ( Math::isZero( value ) ) {
        _x = 0.0f;
        _y = 0.0f;
    } else {
        float inv = 1/value;
        _x *= inv;
        _y *= inv;
    }    
    return *this; 
}

This is rather an older class of mine and this is not the complete class as it has other member functions that are not operators such as normalize, dot, length etc. I just shown the class with its constructors and operators to demonstrate how you might do something similar with your matrix class(s).


As a note of reference if you want to see how a versatile matrix class is implemented there are two major libraries that are worth checking out and read through both their documentations and source files: GLM is a header only library and very easy to setup and use. Eigen on the other hand is a little more involved to setup up and does have a little bit of a learning curve to get use to.

solved C++ Class implementation [closed]