Thread: Overloading * operator

  1. #1
    Registered User
    Join Date
    Dec 2006

    Overloading * operator


    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
    Declare operator* to be a free function, e.g.,
    Vector operator*(const Vector&, const Vector&);

    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:
    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:
    Matrix operator*(const Vector&, const Matrix&);
    Hope you get the idea.
    Last edited by laserlight; 05-13-2007 at 02:33 AM. Reason: Incomplete/incorrect answer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    You still want it to be a free function so you can have
    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, 07: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, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM