0

I don't know how to overload the square brackets operator "[]" that it will both input and output which means I will be able to:

_class ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;

I saw this question and it gave this code:

unsigned long operator [](int i) const    {return registers[i];}
unsigned long & operator [](int i) {return registers[i];}

This did not work for me :-( I tried and did this:

struct coord {
    int x;
    int y;
};

class _map
{
    public:
        struct coord c{3,4};
        char operator[](struct coord) const         // this is supposed to suppor output 
        {
            cout << "this1" << endl;
            return 'x';
        }
        char& operator[](struct coord)                  // this is supposed to support input 
        {
             cout << "this2" << endl;
             return c.x;
        }

        void operator= (char enter) 
        {
              cout << enter;
        }        

};

then I did in the main:

_map ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;

This gave me:

this2
this2

which means I was unable to make two diff functions that will let me make two diff functionalities such as:

ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;

******************BEFORE EDIT**************************

I am trying to override the square operator [] to both input and output info. while the input to the square brackets is a struct.

I tried this inspired by this question:

struct coord {
    int x;
    int y;
};

class _map
{
    public:

        char operator[](struct coord) const         // this is supposed to suppor output 
        {
            cout << "this1" << endl;
            return 'x';
        }
        char& operator[](struct coord)                  // this is supposed to support input 
        {
             cout << "this2" << endl;
             char a = 'a';
             return a;
        }

        void operator= (char enter) 
        {
              cout << enter;
        }        

};

then I did in the main:

_map ppp;
ppp[{1,2}] = 1;
char x = ppp[{1,2}] ;

This gave me:

this2
this2

When changing the input of the input operator to an int all is good:

char operator[](int coord) const         
        {
            cout << "this1" << endl;
            return 'x';
        }

then I did in the main:

_map ppp;
ppp[{1,2}] = 1;
char x = ppp[2] ;

then I get:

this2
this1

This is from my H.W. but I am just asking on stuff that are not the main part of the HW, also I am working on this little thing for a while...

Tomer
  • 531
  • 7
  • 19
  • 1
    It's not clear how you changed `main` after changing your `operator[]`s or what the non-`const` one looks like after the change. – François Andrieux May 03 '18 at 21:04
  • going to edit :-) – Tomer May 03 '18 at 21:05
  • 1
    If you only change one operator to accept `int`, it's no surprise that it's the overload that will be selected if you provide an `int` as the argument. A non-`const` instance will prefer a non-`const` overload if all other things are equal, but it doesn't apply if the `const` overload is the only one that can bind to the arguments you are passing it. – François Andrieux May 03 '18 at 21:09
  • 1
    `char&` you cannot return a reference to a local variable! – stark May 03 '18 at 21:12
  • Ok, now I understand that, thanks – Tomer May 03 '18 at 21:12
  • @stark thanks, by I saw in https://stackoverflow.com/questions/11066564/overload-bracket-operators-to-get-and-set that he did 2 funcs that one outputs a reference and the other is const, so I thought this is a cpp trick to declare one func as the input and the other as output. Now I understand that his thought was wrong :-( – Tomer May 03 '18 at 21:15
  • 1
    No, that's a correct thing to do. You just need to return a reference to some non-local variable. If that's impossible, return a different class with `=` overloaded. – HolyBlackCat May 03 '18 at 21:51
  • @HolyBlackCat I changed the return value to non-local var but still both calls go to "this2". I will edit the question to the current code – Tomer May 03 '18 at 21:58
  • 1
    That's expected. The first overload will be called if the object it's used on is `const`. – HolyBlackCat May 03 '18 at 22:00
  • @HolyBlackCat so basicly there wouldnt be a diff between ppp[{1,2}] = 1; and int x = ppp[{1,2}] ; since both are not const? do you know how to make the functionality different without touching the "main" – Tomer May 03 '18 at 22:04
  • 1
    Why do you want to make it different in the first place? – HolyBlackCat May 03 '18 at 22:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170324/discussion-between-tomer-and-holyblackcat). – Tomer May 03 '18 at 22:10

1 Answers1

0

The answer is all thanks to HolyBlackCat!

The overriding function of the square brackets ("[]") will return a reference like this:

char& operator[](coord c)
{
    return board[c.x][c.y];
}

Therefore we will be able to assign to it a char as it is a reference to some memory slot as follows:

_map ppp;
ppp[{1,2}] = 1;

In the other hand we will be able to retrieve what is inside since the reference is pointing at some char as following:

char x = ppp[{1,2}] ;

This means no need for two overriding funcs as previously thought.

Tomer
  • 531
  • 7
  • 19