c60c-2ca@e260-2a.berkeley.edu (Andrew Choi) (05/03/90)
In article <35925@think.Think.COM> simons@think.com (Joshua Simons) writes: > > I have a question about resolving overloaded function calls in >C++. In Lippman's book, 'The C++ Primer' there is an example on page 155 >which says that the following function call: > > print( 'a' ) ; > >exactly matches the overloading: > > extern void print( char ) ; > > Is this an error? I don't know C++, but in C the type of the >expression 'a' is 'int' not char so I don't see how this could be an >exact match. Is C++ different from C in this regard? No, this is not an error. In C++ version 2.0, a character literal/variable is considered to match perfectly with a formal parameter of type `char'. It will only considered to match an `int' (and higher derived type) if a perfect match is not found. Hope this helps. Andrew Choi Internet Address: c60c-2ca@web.berkeley.edu Standard Disclaimer
randolph@ektools.UUCP (Gary L. Randolph) (05/03/90)
In article <35925@think.Think.COM> simons@think.com (Joshua Simons) writes:
#
# I have a question about resolving overloaded function calls in
#C++. In Lippman's book, 'The C++ Primer' there is an example on page 155
#which says that the following function call:
#
# print( 'a' ) ;
#
#exactly matches the overloading:
#
# extern void print( char ) ;
#
# Is this an error? I don't know C++, but in C the type of the
No, it is not an error.
You are correct when you state the type of 'a' in C, but looking at
Lippman's book again, see appendix C.2:
In C++, the type of a literal character constant is char; in C
it is int.
He goes on to state the purpose for this is to overload on char.
#expression 'a' is 'int' not char so I don't see how this could be an
#exact match. Is C++ different from C in this regard?
^^^^^^^^^^^^^^^Yes.
:*) Gary
beard@ux1.lbl.gov (Patrick C Beard) (05/04/90)
In article <1990May3.030546.1885@agate.berkeley.edu> c60c-2ca@e260-2a (Andrew Choi) writes: #In article <35925@think.Think.COM> simons@think.com (Joshua Simons) writes: #> #> I have a question about resolving overloaded function calls in #>C++. In Lippman's book, 'The C++ Primer' there is an example on page 155 #>which says that the following function call: #> #> print( 'a' ) ; #> #>exactly matches the overloading: #> #> extern void print( char ) ; #> #> Is this an error? I don't know C++, but in C the type of the #>expression 'a' is 'int' not char so I don't see how this could be an #>exact match. Is C++ different from C in this regard? # # No, this is not an error. In C++ version 2.0, a character #literal/variable is considered to match perfectly with a formal #parameter of type `char'. It will only considered to match #an `int' (and higher derived type) if a perfect match is not found. To clarify the point, yes, C++ is different from C in this regard. A character literal is of type "char". This was introduced for exactly this reason: to permit more precise argument type matching for the resolution of which overloaded function to call. ------------------------------------------------------------------------------- - Patrick Beard, Macintosh Programmer (beard@lbl.gov) - - Berkeley Systems, Inc. ".......<dead air>.......Good day!" - Paul Harvey - -------------------------------------------------------------------------------
jimad@microsoft.UUCP (Jim ADCOCK) (05/08/90)
In article <1990May3.030546.1885@agate.berkeley.edu> c60c-2ca@e260-2a (Andrew Choi) writes: >In article <35925@think.Think.COM> simons@think.com (Joshua Simons) writes: .... >> print( 'a' ) ; >> >>exactly matches the overloading: >> >> extern void print( char ) ; .... > No, this is not an error. In C++ version 2.0, a character >literal/variable is considered to match perfectly with a formal >parameter of type `char'. It will only considered to match >an `int' (and higher derived type) if a perfect match is not found. > Right. To make this even clearer, a char const 'a' is of type int in C, but is of type char in C++. For example: sizeof('c') == sizeof(int) in C, whereas sizeof('c') == sizeof(char) and sizeof(char) == 1 /* by definition */ in C++. These definitions may not be recognized correctly by compilers predating 2.0 compatibility.
dsa@dlogics.COM (David Angulo) (05/08/90)
In article <2619@ektools.UUCP>, randolph@ektools.UUCP (Gary L. Randolph) writes: > > In C++, the type of a literal character constant is char; in C > it is int. > He goes on to state the purpose for this is to overload on char. > > > #expression 'a' is 'int' not char so I don't see how this could be an > #exact match. Is C++ different from C in this regard? > ^^^^^^^^^^^^^^^Yes. Did this change in 2.0? I had a problem recently because I had defined a class something like this: class a { a (int); a (char*); }; When I used it, I had forgotton which constructors I had defined, so I coded: char a_char; a an_a (a_char); When I finally tracked it down, I realized that it was using the "int" constructor. I thought "Well, you would think that it would at least give you a warning," and proceeded to make the "char" constructor as well. I was really flabbergasted to see the ERROR message "cannot distinguish a char from an int." This was in 1.2 so has this changed in 2.0? Your Lippman reference says specifically that you CAN overload by an argument of type char. -- David S. Angulo (312) 266-3134 Datalogics Internet: dsa@dlogics.com 441 W. Huron UUCP: ..!uunet!dlogics!dsa Chicago, Il. 60610 FAX: (312) 266-4473