Operator [] overloading

M

Mykhaylo Khodorev

Hi, all
I've created a class derived from CObArray. The declaration of operator
[] 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
 
D

Dave Moore

Mykhaylo Khodorev said:
Hi, all
I've created a class derived from CObArray. The declaration of operator
[] 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

It should be automatic .. that is the whole point of using a reference.

The first is for expressions like:

b=a;

and the second is for expressions like:

a=b;

Under typical circumstances you can use exactly the same code for both
functions. However you really need to give us more details if you want more
in-depth help.

Dave Moore
 
K

Karl Heinz Buchegger

Dave said:
Mykhaylo Khodorev said:
Hi, all
I've created a class derived from CObArray. The declaration of operator
[] 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

It should be automatic .. that is the whole point of using a reference.

The first is for expressions like:

b=a;

and the second is for expressions like:

a=b;


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

Dave Moore

Karl Heinz Buchegger said:
Dave said:
Mykhaylo Khodorev said:
Hi, all
I've created a class derived from CObArray. The declaration of operator
[] 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

It should be automatic .. that is the whole point of using a reference.

The first is for expressions like:

b=a;

and the second is for expressions like:

a=b;


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.

I can see how my explanation may have been a bit unclear, but I don't think
it was incorrect. The point I was trying to make is that only the non-const
version can be used on the left-hand side of an expression without
generating an error, because the temporary returned by value from the const
version is an rvalue. It is of course true that the non-const version will
be called when operator[] is called for a non-const object, or through a
pointer or reference to non-const. I should have been more precise in my
original reply, sorry.

There is one further issue with the OP's example however, since the
operator[] functions there return a pointer (or a reference to a pointer).
I am not completely certain that a temporary pointer value (as returned by
the const version) cannot be used as a lvalue. I have never dealt with this
particular case before, and it may have ramifications I am not aware of.
However my intuition is that it would only mean that attempts to use the
const version on the left hand side of an assignment expression would no
longer generate a compile-time error.

Dave Moore
 
M

Mike Wahler

There is one further issue with the OP's example however, since the
operator[] functions there return a pointer (or a reference to a pointer).
I am not completely certain that a temporary pointer value (as returned by
the const version) cannot be used as a lvalue.

It's not a temporary value. On a hunch, I looked up 'CObArray' at
MSDN. It's an MFC class, representing an array of pointers. (IOW
the array elements themselves are pointers, lvalues).

-Mike
 
M

Mike Jolley

Mykhaylo Khodorev said:
Hi, all
I've created a class derived from CObArray. The declaration of operator
[] 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

operator[] should return a reference, either const or non-const.
The non-const version will automatically allow writing to the referred
datum.

const CObArray& operator [] (int nIndex) const;
CObArray& operator [] (int nIndex);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top