Topic: Operator overloading question
Author: ajoycs@netscape.net
Date: 1998/10/28 Raw View
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 ]
Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/10/28 Raw View
On 28 Oct 1998 18:31:07 GMT, ajoycs@netscape.net <ajoycs@netscape.net> wrote:
>Does what you have look like the following??
>
>class String {
>//...
>public:
>//...
> char& operator[]( int index ) { /* ... */ }
> char operator[]( int index ) { /* ... */ }
>};
In C++, you can overload functions by the constancy of the pointed to
object. So, these two functions are completely different:
char& f(String *);
char f(String const *);
You can't overload by the constancy of the object itself. So try this:
class String {
//...
public:
//...
char& operator[]( int index ) { /* ... */ }
char operator[]( int index ) const { /* ... */ }
};
The const func can also return a "const T&". This is a good idea if
T is a big object.
>This should be *rejected* by the compiler....(according to me :)
Right.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ 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 ]
Author: "Nate Lewis" <nlewis@mindspring.com>
Date: 1998/10/28 Raw View
rama_srinivas@hotmail.com wrote in message
<7138et$mrk$1@nnrp1.dejanews.com>...
>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,).
>However, 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?
I'm surprised that nobody has spoken up with what is, AFAIK, the most
common approach to a string operator[]:
char& operator[](int index);
const char& operator[](int index) const;
The first returns a writable reference; the second (which will be used
on a const string) returns a read-only reference. You want both so as
to be able to use [] on const strings; note that the first version will
be used on a non-const string even if you don't plan to write through
the reference.
I suppose returning char rather than const char& from the second version
might be more common; I prefer it as written above, but I don't think it
matters.
Obc.s.c++: discard with maximum prejudice any compiler confused enough
to compile the above without the const, preferably at high speed towards
your vendor. :)
--
Nate Lewis, MCSD
nlewis@mindspring.com
[ 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 ]
Author: rama_srinivas@hotmail.com
Date: 1998/10/27 Raw View
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?
Thanks in advance,
Srinivas Rama
-----------== 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 ]
Author: "Jeff" <jhaas1@bigred.NOSPAM.unl.edu>
Date: 1998/10/27 Raw View
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
***********
(Remove NOSPAM to reply by e-mail.)
---
[ 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 ]
Author: aitken@coho.halcyon.com (William E. Aitken)
Date: 1995/11/07 Raw View
Consider the following piece of code.
typedef enum
{
e1,
e2
} E;
int main(int argc, char **argv)
{
E e;
e = e2;
return 0;
}
Should it typecheck? My naive reading of the standard suggests that the
expression e = e1 is not type correct, but this conclusion seems too brain
damaged to be believable. What am I missing? Here is my reasoning.
e (and e1, for that matter) are of enumerated type, so overloading
resolution comes into play. There are no user defined overloadings
of operator =, so we need only consider the built in overloadings.
For each arithmetic type L, every promoted arithmetic type R and every
VQ that is either volatile or empty, there is a built in overloading
of operator =
VQ L & operator =(VQ L &, R).
And for every type T and every VQ that is either volatile or empty,
there is a built in overloading
T* VQ & operator =(T* VQ &, T*).
None of these seems to be a viable candidate. There are no implicit
conversions from enumerated types to pointer types, so there doesn't seem to
be any way for any of the T* VQ & operator = (T*VQ&,T*) functions to be viable.
For any arithmetic type L, an _rvalue_ of type E can be converted implicitly
to L, but the result is an _rvalue_ of L, which cannot then be bound to the
a reference to non-const, as required by the first parameter of the functions
VQ L & operator =(VQ L &, R). Moreover, there doesn't seem to be any
implicit conversion that converts an lvalue of enumerated type to an
lvalue of arithmetic type [note that enumerated types are _not_ arithmetic
types]. Thus, the more likely looking VQ L & operator=(VQ L &, R)
overloadings don't appear to be viable either.
Enums seem to be the only possible problem types. Arithmetic types, and
pointer types seem to be adequately handled by the built in overloadings.
Class, structs and unions either have user defined assignment operators
or implicitly defined assignment operators. Assignment to arrays and
functions isn't supposed to work. The only remaining types are enum types.
Bill.
--
William E. Aitken | Formal verification is the
email: aitken@halcyon.com | future of computer science ---
Snail: 8500 148th Ave NE #H1026 Redmond WA | Always has been, always will be.
===============================================================================
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]