Trying to implement a discharged array and overload an operator for him []. I read that you can overload the operator to read and write (if so, how to do it?)
In general, I want to do the following:
Operator [] for this case: cout & lt; & lt; array [0] & lt; & lt; endl;
only on withdrawal
And the [] statement for this case: array [0] = 1;
the operator returns a value that can be changed and output
For each case I want to implement a separate logic
Changed to it:
int operator [] (int index) const {
RETURN DATAARRAY [INDEX];
}
int & amp; Operator [] (int index) {
RETURN DATAARRAY [INDEX];
}
Call in Main:
cout & lt; & lt; SP [0] & lt; & lt; Endl;
SP [0] = 1;
Answer 1, Authority 100%
Something is better than this horror does not come to mind. And I would not write, but since I said in the comments, I need to talk and …
# include & lt; iostream & gt;
#InClude & lt; Iomanip & gt;
#Include & lt; MAP & GT;
Using Namespace STD;
Class Array.
{
Struct Proxy.
{
INT Operator = (Int V)
{
m [s] = v;
RETURN V;
}
Operator int ()
{
if (M.Count (S)) RETURN M [S];
Return 0;
}
Proxy (Array * A, Size_t S): M (A- & GT; M), S (S) {}
MAP & LT; Size_T, Int & GT; & amp; m;
Size_t s;
};
Public:
Array () = default;
Proxy Operator [] (Size_T S)
{
Return Proxy (This, S);
}
Size_t Count () {return M.Size (); }
Private:
MAP & LT; Size_T, Int & GT; m;
};
Int Main (Int Argc, Const Char * Argv [])
{
Array A;
a [5] = 4;
for (int i = 0; i & lt; 7; ++ i) cout & lt; & lt; A [i] & lt; & lt; "";
COUT & LT; & LT; "\ n" & lt; & lt; a.count () & lt; & lt; Endl;
}
I do not like yourself, so I hope someone will come up with something better …