Dave Moore wrote:[color=blue]
>
> "Mykhaylo Khodorev" <ralfeus@chicagocentre.com.ua> wrote in message
> news:cuhsi5$98$1@news.dg.net.ua...[color=green]
> > Hi, all
> > I've created a class derived from CObArray. The declaration of[/color]
> operator[color=green]
> > [] of CObArray looks like:
> > CObArray* operator [] (int nIndex) const;
> > CObArray*& operator [] (int nIndex);
> >
> > I understand first operator is for getting data from array and second
> > one is for writing data to array. But I can't understand how to implement
> > second type of operator. How can I get a data to write to array?
> > Thanks.
> > Mykhaylo
> >[/color]
>
> It should be automatic .. that is the whole point of using a reference.
>
> The first is for expressions like:
>
> b=a[i];
>
> and the second is for expressions like:
>
> a[i]=b;[/color]
Which is exactly not true.
The truth is: You can't differentiate (at least not with the above) between
'reading' and 'writing' through operator[]. One would need a proxy class
to implement this feature.
The const form of the operator is used (as any other const member function)
if it is the only one availabe or if the object by itself is const. The
other one is used in all other cases.
so in
CObArray test;
test[2] = 5;
i = test[2];
The same operator[] (the non const version) is used in both
cases, since 'test' is not const.
Whereas in
const CObArray test;
test[2] = 5;
i = test[2];
again the same operator[] (this time the const version) is used
in both cases.
--
Karl Heinz Buchegger
kbuchegg@gascad.at