-
July 28th, 2008, 10:09 AM
#16
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."
-
July 28th, 2008, 10:12 AM
#17
Re: Assignment on function
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
-
July 28th, 2008, 10:14 AM
#18
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.
-
July 28th, 2008, 10:18 AM
#19
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
-
July 28th, 2008, 10:26 AM
#20
Re: Assignment on function
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.
-
July 28th, 2008, 10:27 AM
#21
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
-
July 28th, 2008, 10:33 AM
#22
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.
-
July 28th, 2008, 10:41 AM
#23
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?
-
July 28th, 2008, 10:55 AM
#24
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
-
July 28th, 2008, 10:51 PM
#25
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]
-
July 28th, 2008, 10:54 PM
#26
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*.
-
July 29th, 2008, 02:23 AM
#27
Re: Assignment on function
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.
-
July 29th, 2008, 02:25 AM
#28
Re: Assignment on function
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|