Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > (newbie) Overloading operator []

Reply
Thread Tools

(newbie) Overloading operator []

 
 
hall
Guest
Posts: n/a
 
      06-30-2003
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[i]=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


--
( - Remove capital X from email to reply - )

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2003
"hall" <(E-Mail Removed)> wrote...
> 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[i]=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[s] 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


 
Reply With Quote
 
 
 
 
John Tsiombikas (Nuclear / the Lab)
Guest
Posts: n/a
 
      06-30-2003
hall wrote:
> 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[i] = new int*[jsize];
for(int j=0; j<jsize; j++) {
a[i][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 --

 
Reply With Quote
 
Christian Jaeger
Guest
Posts: n/a
 
      06-30-2003
templates could do the trick:

template<class T, int n> class array{

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

private:
T data[n];
};

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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-30-2003
"hall" <(E-Mail Removed)> wrote...
> First off: thanks for your quick answer!
>
> Victor Bazarov wrote:
> >
> > It is possible. You need some kind of proxy object[s] 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments