Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

operator for matrix class

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
0
0
GB
Hi Im writing a matrix class and i wondered if anyone knows how to write an operator to assign a value to the array using row and column for index. I had a go but am lost really :

void operator(int Row, int Col) = (double Value) ....

ie

Matrix M = new Matrix(3,3); // creates a 3x3 matrix

M(1,1) = 2;

Thanks in advance.
 
Well, it looks like what you were trying to do was create some strange operator that does a simultaneuous index and assignment. That's way too specialized. What you really want (and what your sample code sugests you want) is an operator that returns the indexed element. Then you can do whatever you want with that element, be it an assignment, an increment, or an output.

You want to define operator() (that's its whole name; like operator= or operator+=) for your class. It will take two indices, most likely some form of integer. It needs to return a reference to the element you want to modify.

Note: good programming practice dictates that you also make a const version of this function that returns a reference to const. I don't know whether or not you want/need to go this far, but that's how it goes.
 
If you would make your operator return a reference to a double value of the matrix, it could be used as element indexing and assignment:

Matrix m;
m(1, 1) = 5;
double x = m(1, 1);

Define the operator like this:
Code:
class Matrix
{
	double& operator()(int row, int col) 
	{  
		// here you should return the appropriate
		// value

		// return value;
	}
};

Greetings,
Tobi
 
Ok thanks.

so given the above reply, I have one question:

What if you have a pointer to a Matrix ? Can i write a different operator for this ?

Matrix *pMatrix = new Matrix(4,4);

pMatrix->(1,1) = 5.0; // produces compile error (syntax error)

thanks in advance
 
No, you can't write an operator like that, but you can handle the situation in a couple different ways.

You can say [tt]( *pMatrix )( 1, 1 )[/tt], which looks a little funny.

You can also say [tt]pMatrix->operator()( 1, 1 )[/tt], which is basically what you were trying to do above, you just got the syntax wrong. This also looks a little funny, but it makes your intent clearer.

Another alternative is to make a function called "at" or something similar that has the exact same effect as operator(). It just looks a little clearer to say [tt]pMatrix->at( 1, 1 )[/tt].
 
Thats a shame,

i've been using
(*pMatrix)(1,1) = 2;

but as you say it looks funny. to the wrong reader this could look like some sort of function pointer.

this is probally where the extensibility of operators ends ..

thanks anyway.
 
Well, operators are mostly mere syntactic sugar, anyway. It's just cleaner and more natural to say "1 + 5" than "add( 1, 5 )". It's an added convenience to be able to overload the operators to work with your classes. It also allows some neat tricks like using the same syntax for pointers and for iterators, making it possible to create generic templates that work with both.

The main problem you have with your indexing operator is that it has a higher precedence than the dereference operator, so you have to put the deref. into more parentheses and it gets messy; it doesn't really have anything to do with operators not being extensible. There's no such thing as a "->+" operator; you still have to do *x + *y. The difference is that you don't need the extra parentheses since addition has a lower precedence than dereference, so it looks a little nicer.

You can always do something like:

[tt]Matrix& m = ( *mPtr );
m( 4, 6 ) = 7;
[/tt]
to make it clear what you're doing without much (or any) extra overhead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top