From 4076412111181604480
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,67a6649d91bbc903
X-Google-Attributes: gidf78e5,public
X-Google-Thread: 109fba,67a6649d91bbc903
X-Google-Attributes: gid109fba,public
From: ajoycs@netscape.net
Subject: Re: Operator overloading question
Date: 1998/10/28
Message-ID: <7179pg$7as$1@nnrp1.dejanews.com>#1/1
X-Deja-AN: 406016363
X-NNTP-Posting-Host: 164.100.9.113
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <7138et$mrk$1@nnrp1.dejanews.com> <7155ks$94u@crcnis3.unl.edu>
X-UID: 0000000001
X-Status: $$$T
X-Http-User-Agent: Mozilla/4.5 [en] (X11; I; SunOS 5.5.1 sun4u)
X-Http-Proxy: 1.0 x4.dejanews.com:80 (Squid/1.1.22) for client 164.100.9.113
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Wed Oct 28 14:30:08 1998 GMT
Newsgroups: comp.lang.c++,comp.std.c++
Originator: clamage@taumet


Hi:

Does what you have look like the following??

class String {
//...
public:
//...
	char& operator[]( int index ) { /* ... */ }
	char  operator[]( int index ) { /* ... */ }
};

This should be *rejected* by the compiler....(according to me :)
Even if the compiler did'nt crib ... how would  you tell the
compiler  which  operator[]  _you_had_in_mind_ when
you  wrote  say, myString[x]   somewhere.??

It most probably must be taking the first definition of operator[]
when it doesnt want to trouble you. This is dangerous though!!!

If you want myString[x] to appear on the lhs of an assignment stmt
you should probably  always return a reference.

But the need for return-by-val is a legit one.
meaning ... it return a l-value only when used as a lhs
of an assignment operator  and  return a  value in all other
cases(like func.args etc..).
But then I feel this prob cant be solved by an operator[]
always returning a ref&.

To me this looks like some design issue...
More discussion may  be needed here.
All suggestions/comments welcome from the "experts".

Ajoy CS.

In article <7155ks$94u@crcnis3.unl.edu>,
  "Jeff" <jhaas1@bigred.NOSPAM.unl.edu> wrote:
> rama_srinivas@hotmail.com wrote:
>
> >Hi,  I am writing a String class which does bounds-checking and dynmaic
> >resizing of the array. I want to overload the indexing operator([]) in the
> >following manners:  char & operator[](int index);  char operator[](int
> >index);  One of them - the first one - returns a reference so that the
> >l-value usage is checked for bounds-overflow(or underflow). The second
> >variant returns a char value which can be assigned to another l-value or
> >passed to functions expecting char value as input(like tolower() etc.,). How
> >ever, the signature of both the functions is the same and this does not
> >compile under VC++. Surprisingly, this compiles fines on a HP system and
> >works too - though I have to check which function is being called in which
> >case. I cannot write different code for Unix/NT since the code base is to be
> >the same. Is there any way that this can be done in C++? If yes, how?
>
> For once VC is correct.  You cannot overload a function (an operator is just a
> special function) by return type.  I'm not sure what the HP compiler is up to,
> but I'm guessing that for some reason it's ignoring one of the two
definitions.
> Since it works correctly, I'd say it's using the return-by-reference version.
>
> I'm a little confused as to what the difference between the two operators is.
> You have char& operator[](), which does a range check and and returns the
> reference so that you can assign to it.  Then you have char operator[](),
which
> _should_ be range-checked, and returns the value of the char.  The way I see
it,
> there is no need for the second version.  Use the first operator to do a range
> check and then return a reference to the appropriate character.  You can
either
> assign to or read from that reference, and the need for the return-by-value
> version is eliminated.
>
> -Jeff



-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




