Speacialized operator for dissimilar types

Discussion in 'C++' started by James, Jul 19, 2005.

  1. James

    James Guest

    I can't find a solution to this one, any suggestions would be welcome.

    Say you have a matrix class specialized with storage type and
    dimensions:

    template<class T, unsigned ROWS, unsigned COLS>
    class Matrix() {
    ...
    };

    You can overload an operation for multiplication of similar specialized
    types:
    Matrix<T,ROWS,COLS> operator*(const Matrix<T,ROWS,COLS>& right) const;

    but can you think of a way to specialize a global function for
    dissimilar types? ie:

    Matrix<float, 3, 1> m1, m3;
    Matrix<float, 3, 3> m2;
    //Initialize operands...
    m3 = m2 * m1;


    TIA for any suggestions!
     
    James, Jul 19, 2005
    #1
    1. Advertising

  2. James

    Alan Johnson Guest

    James wrote:
    > I can't find a solution to this one, any suggestions would be welcome.
    >
    > Say you have a matrix class specialized with storage type and
    > dimensions:
    >
    > template<class T, unsigned ROWS, unsigned COLS>
    > class Matrix() {
    > ...
    > };
    >
    > You can overload an operation for multiplication of similar specialized
    > types:
    > Matrix<T,ROWS,COLS> operator*(const Matrix<T,ROWS,COLS>& right) const;
    >
    > but can you think of a way to specialize a global function for
    > dissimilar types? ie:
    >
    > Matrix<float, 3, 1> m1, m3;
    > Matrix<float, 3, 3> m2;
    > //Initialize operands...
    > m3 = m2 * m1;
    >
    >
    > TIA for any suggestions!
    >


    Here is a test program that I think does what you are trying to do:

    -Alan

    #include <iostream>

    template <typename T, unsigned ROWS, unsigned COLS>
    class Matrix
    {
    } ;

    template <typename T, unsigned A, unsigned B, unsigned C>
    Matrix<T, A, C> operator *(
    const Matrix<T, A, B> & m1,
    const Matrix<T, B, C> & m2)
    {
    std::cout << "Multiplying " << A << 'x' << B << " Matrix by "
    << B << 'x' << C << " Matrix." << std::endl ;
    return Matrix<T, A, C>() ;
    }

    int main()
    {
    Matrix<int, 2, 3> a ;
    Matrix<int, 3, 4> b ;
    Matrix<int, 2, 4> c ;

    c = a * b ;

    return 0 ;
    }
     
    Alan Johnson, Jul 19, 2005
    #2
    1. Advertising

  3. James

    James Guest

    This does exactly what I want. I'll give it a shot, thanks Alan.

    James
     
    James, Jul 19, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    2
    Views:
    592
    Sathyaish
    May 22, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    567
    Henry S. Thompson
    Dec 1, 2005
  3. Jeremy Cowles

    STD types vs C++ intrinsic types

    Jeremy Cowles, Aug 18, 2003, in forum: C++
    Replies:
    5
    Views:
    1,891
    Bob Jacobs
    Aug 19, 2003
  4. Steve Knight
    Replies:
    2
    Views:
    765
    Steve Knight
    Oct 10, 2003
  5. Replies:
    8
    Views:
    368
    Gabriel Genellina
    Nov 14, 2006
Loading...

Share This Page