Overloading operator[]

So I believe I'm on the right track here but I'm not the best when it comes to C++. I need to overload the [] operator for a Matrix class. The project and code is very long so I'll just include the important parts here you can assume I have constructors and other functions working correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Matrix.h

#include <string>
#include <ostream>


// Type definitions 

typedef double       Element_t; //Data type inside the "matrix"
typedef unsigned int Index_t; //Data type used for indexing

class Matrix
{
public:
        Element_t* operator[](Index_t index) const; // I cannot change this
private:
        // Matrix dimensions
	Index_t nRows;
	Index_t nCols;

	// Array to hold matrix data
	// 2D matrix is stored in a 1D array
	// mat[i][j] = elements[i*nCols+j];
	Element_t *elements;
};


That's the important stuff for the header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Matrix.cpp
#include "Matrix.h"
#include <string>
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;

.
.
.

Element_t* Matrix::operator[](Index_t index) const
{
	return &elements[index*nCols];
}

.
.
.


My thoughts here are that "elements" is a one dimensional array holding a 2D matrix. So if I wanted to point to the [i]th row I would take "index" and multiply that by "nCols" and return that array as a pointer to that index. So when I call something like:

 
mat[i][j]


The overloaded operator would return the index to the [i]th row and then the [] operator would return the [j]th "element" after that.

Now that I've typed all this up I think I see my problem. I'm returning 1 element of the array in my code and not a whole "row" like if I had a matrix:

1 2 3
4 5 6
7 8 9

and I wanted mat[1][2] my code is only returning a pointer to "4" the first time and not to the whole row? But I thought the pointer could only point to one element in an array...so now I'm confusing myself. Because the "elements" are stored like:

Elements_t elements = {1,2,3,4,5,6,7,8,9};

So it points to 4 and then the 2nd [] operator does nothing in a sense? As you can tell I'm a little confused with pointers and references. Any advice would be really appreciated.
You can use operator[] on pointers. If p is a pointer then p[i] is equivalent to *(p + i).

So mat[1][2] is the same as *(mat[1] + 2). mat[1] gives you a pointer to the 4. mat[1] + 2 moves the pointer two steps so that it points to the 6.
Last edited on
So more like: return (elements + index * nCols)?

I have to implement it to satisfy a call in the form mat[1][2] so I can't just change my calls to be mat[1] + 2.

Sorry, I keep confusing myself. I've spent too long on this code and my mind is melting.
I didn't mean you should change anything. I thought your code was correct and I tried to explain why.
Last edited on
Ha ok. Thanks. I guess I'm doing something else wrong because its not implementing right. I guess I get to spend hours trying to figure out what the problem is. Thanks for the help.
The signature of your function (which you cannot change??) looks a little bit odd.

Usually you have two versions of operator[]
- a const method which returns a const value, for readonly access
- a non const method which allows change

const Element_t* operator[](Index_t index) const; // const return / const method

and

Element_t* operator[](Index_t index); // no consts

whereas you have one which is half way between?

See for example, std::vector's operator[]

1
2
reference operator[] (size_type n);
const_reference operator[] (size_type n) const;


Andy

PS When I'm working with a 2D array I tend to use row and col (or rowIndex and colIndex) rather than index so I don't forget where I am.

Last edited on
Topic archived. No new replies allowed.