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.
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.
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.
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
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;
}
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.
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.
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?
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.
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]
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*.
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.
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.