(newbie) Overloading operator []

Discussion in 'C++' started by hall, Jun 30, 2003.

  1. hall

    hall Guest

    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


    --
    ( - Remove capital X from email to reply - )
     
    hall, Jun 30, 2003
    #1
    1. Advertising

  2. "hall" <> 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=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
     
    Victor Bazarov, Jun 30, 2003
    #2
    1. Advertising

  3. 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 = 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 --
     
    John Tsiombikas (Nuclear / the Lab), Jun 30, 2003
    #3
  4. 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;
     
    Christian Jaeger, Jun 30, 2003
    #4
  5. "hall" <> wrote...
    > First off: thanks for your quick answer!
    >
    > Victor Bazarov wrote:
    > >
    > > 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
     
    Victor Bazarov, Jun 30, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    449
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    767
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    756
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    584
  5. Replies:
    2
    Views:
    335
Loading...

Share This Page