Overloading * operator

This is a discussion on Overloading * operator within the C++ Programming forums, part of the General Programming Boards category; Hello, I was wondering, can someone please help me with a problem I am having? I have several classes, similar ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    1

    Overloading * operator

    Hello,

    I was wondering, can someone please help me with a problem I am having?

    I have several classes, similar to vectors and matrices, that need to be able to mulitply themselves in the following way.

    Vector vec1;
    Vector vec2;
    Matrix mat1;

    vec2 = vec1 * mat1 * vec2; (1)

    Btw this is just a sample of what I want to do, I haven't worried about transpose and sizes etc. To implement this, I have written the classes to take references and return objects. For example,

    Vector Matrix:perator* (const Vector&);

    But as you can imagine, I can't do the sum (1) above because the return object from the first multiplication is not assigned, so a reference can't be passed to the second multiplication operation.

    Is there a way to be able to do sum (1) without passing in an object rather than a reference?

    Thanks for your help in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,127
    Declare operator* to be a free function, e.g.,
    Code:
    Vector operator*(const Vector&, const Vector&);
    EDIT:

    Sorry, on re-reading I realised that my answer is not quite correct, partially due to my lack of knowledge of matrices and vectors (I'm from my school cohort that had the most sketchy curriculum on matrices ). So pardon me if what I ask seems obvious.

    The rationale for having operator* as a free function is so that an appropriate constructor can be used to (implicitly) convert related types. In this case, however, I am not so sure if makes sense to have a Vector so easily convertible to a Matrix, and/or vice versa.

    So, what does vec1 * mat1 mean? If it should return a Vector, then you can have either:
    Code:
    Vector operator*(const Vector&, const Matrix&);
    // or
    Vector Vector::operator*(const Matrix&) const;
    On the other hand, if it returns a Matrix, then you should have:
    Code:
    Matrix operator*(const Vector&, const Matrix&);
    Hope you get the idea.
    Last edited by laserlight; 05-13-2007 at 03:33 AM. Reason: Incomplete/incorrect answer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You still want it to be a free function so you can have
    Code:
    Vector operator*(const Matrix&, const Vector&);
    or whatever.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21