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 [][] overloading 1

Status
Not open for further replies.

maur3r

Technical User
Aug 28, 2006
34
0
0
PL
I wrote a class "matrix" which uses **double. I am trying to write an operator [][] for this class to be able to perform f.ex
Code:
matrix A;
A[1][1]=2;
Does anybody know how to overload such operator?

Regards, Martin

(OS and comp.)
using Linux and gcc
 
You have to overload it in two steps, each returning a different type.

One of the [ ] returns A[ row ]

The other one takes the result of A[ row ] overload and returns (first result)[ col ], which is your actual data.

Has some alternative thoughts on the matter.


--
 
I don't quite follow the presented option. Would you mind giving a simple example? I would very much appreciate.

Regards, Martin

(OS and comp.)
using Linux and gcc
 
I think the option in that link is much easier and cleaner. You'd basically reference your matrix like this:
Code:
Matrix matrix( numRows, numCols );
...
value = matrix( row, col );
 
Yes but unfortunately I have to create my project compatible with an interface where indexing operator is declared in the way [rows][cols]

Regards, Martin

(OS and comp.)
using Linux and gcc
 
Salem is saying that something that does this:
Code:
Matrix A;
A[1][1] = 2;
is going to be equivalent to something like this:
Code:
Matrix A;
Matrix::Row row = A[1];
row[1] = 2;

There's no such thing as an [tt]operator[][][/tt]. You have to have two implementations of [tt]operator[][/tt]: one that is applied to a matrix and returns a row, and one that is applied to a row and returns an element. Your desired syntax just uses them both in a single step.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top