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...