CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2003
    Posts
    160

    Operator overloading and Ctrsing

    Hi,
    I want to have a functionality similar to that of CString operator +:

    CString c = Cstring a + CString b

    Instead of CString my class is any array. So, I have added the following function as a friend to my class:


    Code:
    template< class T >
    CMatrix< T >& 
    operator + ( CMatrix< T >& a, const CMatrix< T >& b )
    {
    	if( a.GetNumRows() != b.GetNumRows() || 
    		a.GetNumCols() != b.GetNumCols() )
    	{
    		throw2( MSG_BAD_LEN, "CMatrix::operator-" );
    	}
    
    	CMatrix< T > m;
    	for( int r = 0; r < a.GetNumRows(); r++ ) 
    	{
    		for( int c = 0; c < a.GetNumCols(); c++ )
    		{
    			m.SetAt( r, c, a.GetAt( r, c ) + b.GetAt( r, c ) );	
    		}
    	}
    	return( m );
    }
    But it yields the following warning:

    warning C4172: returning address of local variable or temporary
    see reference to function template instantiation 'class CMatrix<double> &__cdecl operator -(class CMatrix<double> &,const class CMatrix<double> &)' being compiled

    Could someone please help?
    In addition, I am having trouble trying to get the pointer to the STL vector object. Is it possible to get a pointer to it, or not, so that I can make a copy using memset?

    Thanks
    Zeato

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Operator overloading and Ctrsing

    Quote Originally Posted by dr_zeato
    But it yields the following warning:

    warning C4172: returning address of local variable or temporary

    Could someone please help?
    Don't return a reference or pointer to a local object. That's the problem.
    Code:
    CMatrix< T >&  operator + ( ... )
    {
        //...
        CMatrix< T > m;
        //...
       return( m );
    }
    This leads to undefined behavior in C++, which is why you get the warning.

    Just return an object.
    Code:
    CMatrix< T >  operator + ( ... )
    {
        //...
        CMatrix< T > m;
        //...
       return( m );
    }
    In addition, I am having trouble trying to get the pointer to the STL vector object.
    What "STL vector object"?
    Is it possible to get a pointer to it, or not,
    You can get a pointer to any type, so long as the object is in scope.
    so that I can make a copy using memset?
    Given that memset() does not work for non-POD types, I don't recommend it.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2003
    Posts
    160

    Re: Operator overloading and Ctrsing

    What "STL vector object"?
    You can get a pointer to any type, so long as the object is in scope.Given that memset() does not work for non-POD types, I don't recommend it.

    Regards,

    Paul McKenzie
    Thanks for the reply. I have something like:

    vector< vector< T > >

    which is an array of vectors. Now for making a copy of such an object, either I have to go into each vector in this array and make a memberwise copy. But, on the other hand, I may also use memcpy on the pointer to the data in each vector< T > with a loop only on the outer "vector".

    By the way if I want to copy double* p to double* q, what is faster:
    a memcpy or a loop for the entire array.

    Thanks
    Zeato

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Operator overloading and Ctrsing

    Quote Originally Posted by dr_zeato
    Thanks for the reply. I have something like:

    vector< vector< T > >

    which is an array of vectors. Now for making a copy of such an object, either I have to go into each vector in this array and make a memberwise copy.
    Why do you think you need to do that? Vector has an overloaded operator =.
    Code:
    #include <vector>
    
    int main()
    {
       std::vector<std::vector<int> > v1;
       std::vector<std::vector<int> > v2;
    
        // make v1 same as v2
      v1 = v2;
    }
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Operator overloading and Ctrsing

    What is wrong with vector's "=" operator? You can just do
    Code:
    vector<vector<int> > a, b;
    a = b;
    This will do a member-wise copy for you.

    By the way if I want to copy double* p to double* q, what is faster:
    a memcpy or a loop for the entire array.
    Depends on the quality of the compiler and the standard library. Most of the time memcpy is faster, but if the library is not great and the compiler is great, the loop may be better.

    In any case, there are already lots of existing matrix classes that are out there and are probably faster than something you can throw together in a few days. Plus they are already debugged, which is a great help. For any serious matrix class, I would not recommend to use vector<vector< > > anyways, since this has problems with memory contiguity (and hence cache misses etc).
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  6. #6
    Join Date
    Oct 2003
    Posts
    160

    Re: Operator overloading and Ctrsing

    If you can recommend any Matrix library, I would appreciate it. I know that anything that I implement in a few days is going to haunt me, if not extensively tested.
    Thanks
    Zeato

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Operator overloading and Ctrsing

    Quote Originally Posted by dr_zeato
    If you can recommend any Matrix library, I would appreciate it.
    http://www.google.com/search?hl=en&l...2B&btnG=Search

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Operator overloading and Ctrsing

    I haven't used any of them really, but I've tried Blitz++ and it's indeed pretty fast. But there is a pretty vast choice of good implementations, so you may want to have a look at several of them.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured