(newbie) Overloading operator []

H

hall

Hi all.

I'm in the process of writing a 3 dim array class and initially, i
wanted to overload the [] operator so that each element could be
accessed i a way similar to how you access a simple C-style array. Example:

Array[0][1][2];

(where Array is an instance of this class) should returns a reference to
the element with indicies (0,1,2), comparable to how one adresses a
C-style array (here in 2 dim and using int:s):

int *C_array[5];
for (int i=0;i<5;i++) {C_array=new[5]};
array[0][1]; // <- this is what i mean by C-style

The more i think about it, the less do I think it is possible, but
perhaps it is? Is there a simple way of obtaining this syntax or should
I disregard the aestetics and walk the simle way ( Ie using a syntax
like: Array.getRefToElement(0,1,2); to get a reference) ?

regards
hall
 
V

Victor Bazarov

hall said:
I'm in the process of writing a 3 dim array class and initially, i
wanted to overload the [] operator so that each element could be
accessed i a way similar to how you access a simple C-style array. Example:

Array[0][1][2];

(where Array is an instance of this class) should returns a reference to
the element with indicies (0,1,2), comparable to how one adresses a
C-style array (here in 2 dim and using int:s):

int *C_array[5];
for (int i=0;i<5;i++) {C_array=new[5]};
array[0][1]; // <- this is what i mean by C-style

The more i think about it, the less do I think it is possible, but
perhaps it is? Is there a simple way of obtaining this syntax or should
I disregard the aestetics and walk the simle way ( Ie using a syntax
like: Array.getRefToElement(0,1,2); to get a reference) ?


It is possible. You need some kind of proxy object that will
help to get to the element. Usually, N dimensions need N-1 proxy
objects. First [] will return proxy_1. The proxy_1 class should
have [] overloaded too, to return proxy_2, etc., until proxy_Nm1's
overloaded operator[] will return a reference. I am fairly sure
you can find something on the web.

Of course, operator()(int,int,int) could be easier. operator()
is what is known as "function call operator".

Victor
 
J

John Tsiombikas (Nuclear / the Lab)

hall said:
Hi all.

I'm in the process of writing a 3 dim array class and initially, i
wanted to overload the [] operator so that each element could be
accessed i a way similar to how you access a simple C-style array. Example:

Array[0][1][2];


Just tried this quickly and seems like working:

---------------- code snippet --------------------
#include <iostream>

class MyArray {
public:
int ***a;

MyArray(int isize, int jsize, int ksize) {
a = new int**[isize];
for(int i=0; i<isize; i++) {
a = new int*[jsize];
for(int j=0; j<jsize; j++) {
a[j] = new int[ksize];
}
}
}

int **operator[](int index) {
return (int**)a[index];
}
};

int main() {
MyArray array(5, 5, 5);

array[4][3][2] = 5;

std::cout << array[4][3][2] << std::endl;
return 0;
}

-------------- code ends -----------
of course this needs a destructor and a way to know the size of each
dimension (3 private variables) etc etc... but you get the point

-- Nuclear / the Lab --
 
C

Christian Jaeger

templates could do the trick:

template<class T, int n> class array{

public:
T& operator[](int i) {return data;}

private:
T data[n];
};

//and if you want to:
typedef array<array<array<int, 5>,5>, 5> Array3;
 
V

Victor Bazarov

hall said:
First off: thanks for your quick answer!

Victor said:
It is possible. You need some kind of proxy object that will
help to get to the element. Usually, N dimensions need N-1 proxy
objects. First [] will return proxy_1. The proxy_1 class should
have [] overloaded too, to return proxy_2, etc., until proxy_Nm1's
overloaded operator[] will return a reference. I am fairly sure
you can find something on the web.


Yes, I had an idea that it might work, but it seemed more complicated
than i like my code to be.
Of course, operator()(int,int,int) could be easier. operator()
is what is known as "function call operator".

Well, i don't think that it would make my code look better than eg
'instance.get(int,int,int)', but while on the subject of the function
call operator, I'd like to ask a question on it. In my reference book,
"C++: the complete reference" (Schildt), it says that

"When you overload the () function call operator, you are not, per se,
creating a new way to call a function. Rather, you are creating an
operator function that can be passed an arbitrary number of parameters."

What does he mean by that?


Who knows? If you spent money on this book, report it as capital
loss and throw this book in a dumpster. Never buy anything Schildt
writes.
Is there a difference between the two member
functions
double operator () (int a, int b){//do something};
and
double fun(int a, int b){//do something};
other than the way you call them in your program (Instance(1,2) vs
Instance.fun(1,2))?

Well, I don't know what to tell you. Is there a difference
between

void foo() {}

and

void bar() {}

except that one you call blah.foo() and the other blah.bar()?

Operator "function call" is extremely important. It gives C++
programmers the ability to use objects with template functions
where pointers to functions could be used. See more in a GOOD
C++ book -- look for "functor objects".
And what about these "...arbitrary number of arguments?" Isn't this the
same for both of them, that you have to overload a version for each
number of argumentss you want to pass, ie
double operator() (int a){};
double operator() (int a, int b){};
...
double fun(int a){};
double fun(int a, int b){};
...

I don't know what he means by that. Yes, you could have more
than one operator() in your class.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top