matrix multiplication - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27

Thread: matrix multiplication

  1. #16
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,243

    Re: matrix multiplication

    Your copy constructor and copy assignment operator should have the parameter as const Matrix&. After all, it should make sense to copy a matrix that is constant.

    I notice that the copy assignment operator copies the row count and column count, i.e., the number of rows and columns of the source matrix could differ from the destination matrix. However, it makes no attempt to allocate space if the source matrix has more rows or columns than the destination matrix.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  2. #17
    Join Date
    Mar 2010
    Posts
    13

    Re: matrix multiplication

    Thanks for your information.

    Initially, I had them as const Matrix&, however, as I am using the overloaded () operator inside the constructor and assignment operator, I am not allowed to use const.
    compilier error:
    Code:
     error: passing 'const Matrix' as 'this' argument of 'float& Matrix::operator()(int, int)' discards qualifiers
    maybe it's just that my () operator is incorectly overloaded:
    Code:
    float& Matrix::operator()(const int nRow, const int nCol)
    {
        return m_pfMatrix[nRow * m_nColCount + nCol];
    }
    as for the copy assignment operator, I'm gonna fix it
    thanks for your input!

    regards,

    replax

  3. #18
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,243

    Re: matrix multiplication

    It sounds like you need to const overload operator(), i.e.,
    Code:
    float& Matrix::operator()(const int nRow, const int nCol)
    {
        return m_pfMatrix[nRow * m_nColCount + nCol];
    }
    
    const float& Matrix::operator()(const int nRow, const int nCol) const
    {
        return m_pfMatrix[nRow * m_nColCount + nCol];
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #19
    Join Date
    Mar 2010
    Posts
    13

    Re: matrix multiplication

    I thought of that, but then I would not be able to modify the matrix using
    Code:
    Matrix(nRow, nCol) = X;
    is there another way of doing this?

  5. #20
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,243

    Re: matrix multiplication

    Quote Originally Posted by replax
    I thought of that, but then I would not be able to modify the matrix using
    Notice that there are two versions of operator().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #21
    Join Date
    Mar 2010
    Posts
    13

    Re: matrix multiplication

    Ah ok I thought you ment to replace it. Anyway, I figured that it works like that:

    Code:
    float& Matrix::operator()(const int nRow, const int nCol) const
    {
        return m_pfMatrix[nRow * m_nColCount + nCol];
    }
    thank you very much for your input.

    replax

  7. #22
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,243

    Re: matrix multiplication

    Yeah, though it may be better to have the const version return a const reference.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #23
    Join Date
    Mar 2010
    Posts
    13

    Re: matrix multiplication

    In which case would it get called?

  9. #24
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,243

    Re: matrix multiplication

    When the Matrix object is const, or when you are accessing the Matrix object through a const reference or a pointer to a const Matrix.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #25
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: matrix multiplication

    As laserlight pointed out, the operator= needs to reallocate memory when the matrix size is changing. One easy way to do this is using the copy-and-swap trick that Paul demonstrated earlier.

    It bears repeating that std::vector should be preferred to dynamic arrays whenever possible, because it's both safer and easier to work with.

    One other point worth mentioning----you should not assume that returning a matrix from a function by value will invoke the copy constructor to create a copy, and the destructor of the matrix that's local to the function. It might; that's the behavior to expect, certainly; but don't assume it must do that, because one common form of compiler optimization tries to remove such unnecessary copies using return value optimization. Whatever it does end up doing will be equivalent to the above, but only if your copy constructor and operator= really do create logical copies. So don't try to get clever with those functions.

  11. #26
    Join Date
    Mar 2010
    Posts
    13

    Re: matrix multiplication

    This is the almost final program, thanks for your help!

    http://rapidshare.com/files/369676516/matrix.zip

    @Lindley, thanks for the information on return values and copy constructors!
    as for the vector, it was a kind of "requirment" to do it with arrays. But I agree that using a std::vector would be better!
    Last edited by replax; March 29th, 2010 at 03:29 PM.

  12. #27
    Join Date
    Apr 1999
    Posts
    27,422

    Re: matrix multiplication

    Quote Originally Posted by replax View Post
    This is the almost final program, thanks for your help!

    http://rapidshare.com/files/369676516/matrix.zip

    @Lindley, thanks for the information on return values and copy constructors!
    as for the vector, it was a kind of "requirment" to do it with arrays. But I agree that using a std::vector would be better!
    Yes, usage of vector removes the need to write a copy ctor/assignment op (and destructor).

    Also, as Lindley pointed out, your copies should be logically equivalent. In other words, if you replace the original with the copy, the copy must behave exactly the same as the original, regardless of the path of execution your code will take.

    A source of bugs is writing copy/assignment functions that really are not making true copies of the object. This is called coding with side-effects. This type of coding is to be avoided when it comes to copy/assignment, as you don't really know when a copy will be made, due to the optimizations a compiler may use.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 29th, 2010 at 11:34 PM.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center