double scripting operator !!!

Overload the operator () to perform the double scripting operations. for example, in a 3-by-5 Matrix called a the programmer could write a (1,3) to access the elemet at row 1 and column 3. there should be 2 versions of operator()- one tht returns int& so an element of Matrix can be used as an lvalue and one tha returns const int& so an element of a matric can be used as an r value .

This is wht i have done ?
is it okai??

1
2
3
4
5
6
7
8
9
10
int & Matrix::operator()(int row, int col)//lvalue; return array2D[row][col];
{
	if(row<= 9 || row>=0 && cols<=9 || cols>=0)
		return(array2D[rows][cols]);
}
const int & Matrix::operator()(int row, int col) const//rvalue; return// array2D[row][col];
{
	if(row<=9 || row>=0 && cols<=9 || cols>=0)
		return(array2D[rows][cols]);
}
Yes.

Personally, I prefer not having boundschecking inside such a low level function, but it might be easier/better for your application.
The argument is named col but in the function you use the name cols. Is that because you have two member variables rows and cols that stores the size of the matrix? In that case shouldn't you test row against rows and col against cols?

Your function doesn't return anything if the condition is false. You should probably do something in that case. return a dummy value (I don't fancy this), throw an exception, or put the condition inside an assert instead of the if statement.
Peter87 is absolutely right; I scanned over the code too quickly.

That is also the reason I don't like bounds-checking inside such a low level function: even if you can find a useful error return value, you'd still have to check against it in the calling code. Might as well simply check in the calling code. It's easier, more efficient and less error-prone.
Topic archived. No new replies allowed.