[] overload

Discussion in 'C++' started by fox, Dec 30, 2003.

  1. fox

    fox Guest

    Hi
    I've two classes

    class node {
    ....
    node operator[](int idx);
    }

    and
    class grid {
    .....
    node operator[](int idx);
    }

    but when i do :
    grid *one=new grid();
    cout << grid[1][1]
    it uses operator [] for grid class, (which returns a node )
    and doesn't use an overloaded one for the returned node value;
    Is there a way to force him ?
    or do i do sth wrong ?

    Cheers
     
    fox, Dec 30, 2003
    #1
    1. Advertising

  2. fox

    Jeff Schwab Guest

    fox wrote:
    > Hi
    > I've two classes
    >
    > class node {
    > ...
    > node operator[](int idx);
    > }
    >
    > and
    > class grid {
    > ....
    > node operator[](int idx);
    > }
    >
    > but when i do :
    > grid *one=new grid();
    > cout << grid[1][1]
    > it uses operator [] for grid class, (which returns a node )
    > and doesn't use an overloaded one for the returned node value;
    > Is there a way to force him ?
    > or do i do sth wrong ?


    How do you know it's wrong? Did you overload operator << (
    std::eek:stream&, node const& )? Is indexing a node really supposed to
    give another node?

    Please post a small program to show the problem. Someone here probably
    can show you what has gone wrong, but it is very difficult to tell from
    the pseudocode.

    -Jeff
     
    Jeff Schwab, Dec 30, 2003
    #2
    1. Advertising

  3. fox

    Dan Cernat Guest

    "fox" <> wrote in message
    news:bsrrm3$3im$...
    > Hi
    > I've two classes
    >
    > class node {
    > ...
    > node operator[](int idx);
    > }
    >
    > and
    > class grid {
    > ....
    > node operator[](int idx);
    > }
    >
    > but when i do :
    > grid *one=new grid();
    > cout << grid[1][1]

    ^^^^^^^^^^^^^^^^^^^^^^^^^^ that could not possibly compile
    if you meant cout << one[1][1];
    then you have some problems here:
    1. you allocated only 1 grid element so one[1] is past the array boundaries.
    it should have been one[0][1]

    2. one is a pointer and when someone does one[n] it actually gets *(one + n)

    so what you should have done is:

    grid myGrid;
    // put here code to fill the collection of nodes
    // then do:

    cout << grid[1][1];

    assuming everything else is working

    > it uses operator [] for grid class, (which returns a node )
    > and doesn't use an overloaded one for the returned node value;
    > Is there a way to force him ?
    > or do i do sth wrong ?
    >
    > Cheers


    Dan
     
    Dan Cernat, Dec 30, 2003
    #3
  4. On Tue, 30 Dec 2003 13:40:34 +0100, fox wrote:

    > Hi
    > I've two classes
    >
    > class node {
    > ...
    > node operator[](int idx);
    > }
    >
    > and
    > class grid {
    > ....
    > node operator[](int idx);
    > }
    >
    > but when i do :
    > grid *one=new grid();
    > cout << grid[1][1]


    cout << (*grid)[1][1];

    You want to act on the contents of the pointer, not the pointer itself.

    HTH,
    M4
     
    Martijn Lievaart, Dec 30, 2003
    #4
  5. fox

    Dan Cernat Guest

    err...

    "Dan Cernat" <> wrote in message
    news:...

    > so what you should have done is:
    >
    > grid myGrid;
    > // put here code to fill the collection of nodes
    > // then do:
    >
    > cout << grid[1][1];

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^make it
    cout << myGrid[1][1];

    > Dan
    >

    Dan
     
    Dan Cernat, Dec 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. Eph0nk

    Error: Overload resolution failed

    Eph0nk, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,890
    Eph0nk
    Oct 23, 2003
  2. MattB

    Overload

    MattB, Dec 22, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    566
    MattB
    Dec 23, 2003
  3. Babs
    Replies:
    0
    Views:
    3,095
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    398
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    284
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page