CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28
  1. #16
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Assignment on function

    I think every one here is talking about something different than you.
    Yes you can use (,) for assignment with the matrix class shown above.

    I believe you are trying to use that syntax with a c-style array, which does not make sense.

    I would suggest you post your matrix class so it can be fixed, or delete it and
    use the one provided here.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  2. #17
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Assignment on function

    Quote Originally Posted by grahamr (work)
    Does it matter what it is? It doesn't change the fact that using () to index into the array doesn't allow assignment (or I can't use assignment).
    Because a reference is returned for the non-const method you can write something like this:

    Code:
    Array2D<int> a( 4,4 );
    
    a( 1,2 ) = 12;
    The benefits of using a class is data encapsulation, once you move everything into the class you don´t have to track additional attributes yourself (like row/column extents).
    You can easily extend the example by methods to query the number of columns or rows, add iterator support for STL compliance, range checking before addressing elements and so on.
    Furthermore, a raw array decays to a simple pointer, when passing an Array2D by reference you still know it´s an Array2D and the client code can query its attributes.
    Last edited by GNiewerth; July 28th, 2008 at 10:17 AM.
    - Guido

  3. #18
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    I thought it was fairly clear from my first post as to what I was referring to, irrespective whether my code is right or wrong.

  4. #19
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    My abbreviated class:
    Code:
    const int cols = 4;
    const int rows = 4;
    
    class matrix  
    {
    public:
    	float m[16];	// m[col][row] -- same as opengl
    	
    	// set to identity
    	matrix( ) 
    	{
     		for ( int col=0; col<cols; col++ ) 
     		{
     			for ( int row=0; row<rows; row++ ) 
     				m[( col * cols ) + row] = 0.0f;
     			m[( col * cols ) + row] = 1.0f;
     		}
    // non-working
    //		for ( int col=0; col<cols; col++ ) 
    //		{
    //			for ( int row=0; row<rows; row++ ) 
    //				m( col, row ) = 0.0f;
    //			m( col, row ) = 1.0f;
    //		}
    	}
    
    // ... snip ...
    	
    	float& operator()( const int col, const int row )
    	{
    		return m[( col * cols ) + row];
    	}
    	
    	float operator()( const int col, const int row ) const
    	{
    		return m[( col * cols ) + row];
    	}
    
    // ... snip ...
    }; // class matrix

  5. #20
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Assignment on function

    Quote Originally Posted by grahamr (work)
    Okay. How about setting up and identity matrix in the constructor?
    Code:
    	for ( int col=0; col<cols; col++ ) 
    	{
    		for ( int row=0; row<rows; row++ ) 
    			m( col, row ) = 0.0f;
    		m( col, row ) = 1.0f;
    	}
    Ok, try doing this instead:

    Code:
    matrix& me = *this;
    
    for ( int col=0; col<cols; col++ ) 
    	{
    		for ( int row=0; row<rows; row++ ) 
    			me( col, row ) = 0.0f;
    		me( col, row ) = 1.0f;
    	}
    Last edited by Zaccheus; July 28th, 2008 at 10:29 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  6. #21
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Assignment on function

    Or
    Code:
    		for ( int col=0; col<cols; col++ ) 
    		{
    			for ( int row=0; row<rows; row++ ) 
    				operator()( col, row ) = 0.0f;
    			operator()( col, row ) = 1.0f;
    		}
    Although Zaccheus's solution is neater.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #22
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Assignment on function

    The important thing is that the function call operator must be applied to the whole class instance, not to the internal data member.
    My hobby projects:
    www.rclsoftware.org.uk

  8. #23
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    That is a whole lot closer to what I was talking about - makes the code a whole lot more readable, no?

  9. #24
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Assignment on function

    Are you using VC6? I had to turn off 'Force conformance in for loop scope' in VC2008 to get it to compile.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  10. #25
    Join Date
    Jun 2008
    Location
    Kuala Lumpur, Malaysia
    Posts
    68

    Re: Assignment on function

    Well, you can allow Matrix[row][col] syntax for column-major matrices also. Here's the code
    Code:
    const int cols = 4;
    const int rows = 4;
    
    
    class column_vector
    {
    private:
    	float* m_pStart;
    
    public:
    	column_vector( float* pStart )
    		: m_pStart(pStart) {}
    
    	float& operator [] ( int col )
    		{ return m_pStart[col * rows]; }
    };
    
    
    
    class matrix  
    {
    public:
    	float m[16];
    
    	// ... snip ...
    	column_vector operator [] ( int row )
    		{ return column_vector( &m[row] ); }
    };
    
    
    blah blah
    matrix Matrix;
    Matrix[1][2] = 10; // Matrix[ROW][COL] is identical to Matrix.m[ROW + COL * rows]

  11. #26
    Join Date
    Jun 2008
    Location
    Kuala Lumpur, Malaysia
    Posts
    68

    Re: Assignment on function

    To be complete, you might have to add one class, const_column_vector which is identical to column_vector class except that it holds const float*.

  12. #27
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    Quote Originally Posted by JohnW@Wessex
    Are you using VC6? I had to turn off 'Force conformance in for loop scope' in VC2008 to get it to compile.
    Yep.

  13. #28
    Join Date
    Jan 2006
    Posts
    344

    Re: Assignment on function

    Quote Originally Posted by muse1987
    To be complete, you might have to add one class, const_column_vector which is identical to column_vector class except that it holds const float*.
    But since you can do assignments on operator() - it was I who was doing it wrong - [][] becomes a bit of a mute point and a lot more hassle than two functions.

    Though I would concede the point that it does make the action perfectly clear.

    G.

Page 2 of 2 FirstFirst 12

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