Left/right side of assignment

H

hari4063

I try to write class with overloaded operator [] but class must know is
operator [] used for read or write. I simple write operator [] that
returns Dummy class that have overloaded = and overloaded conversion, so i
can find if operator [] is used for writing or reading. Is any chance to
implement this with another way?

Thanks,
Zaharije Pasalic
 
V

Victor Bazarov

hari4063 said:
I try to write class with overloaded operator [] but class must know is
operator [] used for read or write.
Why?

I simple write operator [] that
returns Dummy class that have overloaded = and overloaded conversion, so i
can find if operator [] is used for writing or reading. Is any chance to
implement this with another way?

What you're proposing should work fine. Strange requirement, though.

Victor
 
H

hari4063

I write simple map-like (like STL) class. If i write something like:

a = map["abc"]

i wanna to read value assigned to "abc". But when i write

map["abc"] = a;

i wanna to add new value "a" to map?
 
V

Victor Bazarov

hari4063 said:
I write simple map-like (like STL) class. If i write something like:

a = map["abc"]

i wanna to read value assigned to "abc".

What if the 'map' doesn't have a value with "abc" key?
But when i write

map["abc"] = a;

i wanna to add new value "a" to map?

OK, I understand. The apparent difference between your "map" and
the standard template map is that std::map will insert a new entry
into the storage when you use operator[].

V
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

hari4063 said:
I try to write class with overloaded operator [] but class must know is
operator [] used for read or write. I simple write operator [] that
returns Dummy class that have overloaded = and overloaded conversion, so i
can find if operator [] is used for writing or reading. Is any chance to
implement this with another way?

You can additionally write an operator [ ] const that returns directly the
value.
 
H

hari4063

YAP! Ok, this question is not so important, but i like to find another ways
(mostly complicated) to write something in C++ :). Whatever, thanks a lot!

Best,
Zaharije Pasalic
 
D

DaKoadMunky

I try to write class with overloaded operator [] but class must know is
operator [] used for >>read or write.
Why?
I simple write operator [] that returns Dummy class that have overloaded =
and overloaded >>conversion, so i can find if operator [] is used for writing
or reading.
Strange requirement, though.

Scott Meyers in "More Effective C++" had an example of a copy-on-write
reference counted string class. When implementing operator[] he needed to know
if it was for a read or a write given that no special action need be taken for
a read but special action need be taken for a write.

Bjarne Stroustrup also used this technique in either D&E or TCPPPL. I just
spent a few minutes trying to find exactly which one and where but was unable
to find it.
 
R

Ron Natalie

Julián Albo said:
hari4063 wrote:

I try to write class with overloaded operator [] but class must know is
operator [] used for read or write. I simple write operator [] that
returns Dummy class that have overloaded = and overloaded conversion, so i
can find if operator [] is used for writing or reading. Is any chance to
implement this with another way?


You can additionally write an operator [ ] const that returns directly the
value.
Which will do nothing for him. That operator will be invoked only if
the object is const.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top