473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Operator [] overloading

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
Jul 23 '05 #1
5 1657

"Mykhaylo Khodorev" <ra*****@chicagocentre.com.ua> wrote in message
news:cu*********@news.dg.net.ua...
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[i];

and the second is for expressions like:

a[i]=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
Jul 23 '05 #2
Dave Moore wrote:

"Mykhaylo Khodorev" <ra*****@chicagocentre.com.ua> wrote in message
news:cu*********@news.dg.net.ua...
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[i];

and the second is for expressions like:

a[i]=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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at...
Dave Moore wrote:

"Mykhaylo Khodorev" <ra*****@chicagocentre.com.ua> wrote in message
news:cu*********@news.dg.net.ua...
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[i];

and the second is for expressions like:

a[i]=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
Jul 23 '05 #4
"Dave Moore" <dt*****@email.unc.edu> wrote in message
news:37*************@individual.net...
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
Jul 23 '05 #5

"Mykhaylo Khodorev" <ra*****@chicagocentre.com.ua> wrote in message
news:cu*********@news.dg.net.ua...
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);
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.