By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Stroustrup 5.9 exercise 6 (char& as argument)

P: n/a
this is the code:

------------------------------------------------------------------
#include <iostream>

void g(char&){};
void h(const char&) {};

int main() {
char c;
unsigned char uc;
signed char sc;

g(c);
// g(uc);
// g(sc);
// g('a');
// g(49);
//g(3300);

h(c);
h(uc);
h(sc);
h('a');
h(49);
h(3300); }
------------------------------------------------------------------------------

i have 2 questions:

1.) if i can call /g(c)/, why can't i call /g(uc)/ or /g(sc)/ without
any error?

2.) if i compile with "g(uc)", i get an error saying:
"invalid initialization of reference of type 'char&' from
expression of type 'unsigned char'"

BUT nothing wrong happens for "h(uc)". same for all other arguments c,
sc, 49, 3300. why?

Nov 8 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
i wanted to tell that i got the answer to my 2nd question:
BUT nothing wrong happens for "h(uc)". same for all other arguments c,
sc, 49, 3300. why?
as /const char&/ does implicit conversion.

but still 1st question still exist:
1.) if i can call /g(c)/, why can't i call /g(uc)/ or /g(sc)/ without any error?
if i compile with "g(uc)", i get an error saying:

"invalid initialization of reference of type 'char&' from expression of
type 'unsigned char'"

Nov 8 '06 #2

P: n/a

arnuld wrote in message
<11**********************@e3g2000cwe.googlegroups. com>...
>i wanted to tell that i got the answer to my 2nd question:
>BUT nothing wrong happens for "h(uc)". same for all other arguments c,
sc, 49, 3300. why?

as /const char&/ does implicit conversion.

but still 1st question still exist:
>1.) if i can call /g(c)/, why can't i call /g(uc)/ or /g(sc)/ without any
error?
>
if i compile with "g(uc)", i get an error saying:

"invalid initialization of reference of type 'char&' from expression of
type 'unsigned char'"
If you go to the store and tell the guy, "I want an apple", and he hands you
an orange, you would tell him, "NO, I said an apple!". That's what the
compiler is telling you. "Hey, you promised me an 'char', but you tried to
give me an 'unsigned char' instead".

Later you'll learn about 'casting'. That tells the compiler, "Shut up, I know
what I am doing.".

--
Bob R
POVrookie
Nov 8 '06 #3

P: n/a
BobR wrote:
[..]
If you go to the store and tell the guy, "I want an apple", and he
hands you an orange, you would tell him, "NO, I said an apple!".
That's what the compiler is telling you. "Hey, you promised me an
'char', but you tried to give me an 'unsigned char' instead".
Probably useful to mention that 'char', 'signed char' and 'unsigned
char' are three distinct types in C++.
Later you'll learn about 'casting'. That tells the compiler, "Shut
up, I know what I am doing.".
Extending your metaphor, you ask for an apple, the guy picks an orange,
paints it red, polishes it, pushes a short stick into it, and says,
"here, shut up and bite into it".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 8 '06 #4

P: n/a
BobR wrote:
If you go to the store and tell the guy, "I want an apple", and he hands you
an orange, you would tell him, "NO, I said an apple!". That's what the
compiler is telling you. "Hey, you promised me an 'char', but you tried to
give me an 'unsigned char' instead".
ok & Stroustrup says (section 4.3, 4.4)

1.) plain int is always /signed/.
2.) is a char signed or unsigned? unfortunately, which choice is made
for /plain char/ is implementation defined.

so what i am saying /g(c)/ must have been converted to either signed or
unsigned char and if it is converted then one of them / g(uc) or g(sc)
/ must work. i mean:

1.) char is converted to signed char. in this case g(sc) must work
2.) char is converted to unsigned char. in this case g(uc) must work

did you get my point.
Later you'll learn about 'casting'. That tells the compiler, "Shut up, I know
what I am doing.".
haa....haa...

Nov 8 '06 #5

P: n/a
* arnuld:
>BobR wrote:
If you go to the store and tell the guy, "I want an apple", and he hands you
an orange, you would tell him, "NO, I said an apple!". That's what the
compiler is telling you. "Hey, you promised me an 'char', but you tried to
give me an 'unsigned char' instead".

ok & Stroustrup says (section 4.3, 4.4)

1.) plain int is always /signed/.
2.) is a char signed or unsigned? unfortunately, which choice is made
for /plain char/ is implementation defined.

so what i am saying /g(c)/ must have been converted to either signed or
unsigned char and if it is converted then one of them / g(uc) or g(sc)
/ must work. i mean:

1.) char is converted to signed char. in this case g(sc) must work
2.) char is converted to unsigned char. in this case g(uc) must work

did you get my point.
Although 'char' is either signed or unsigned (depending on the compiler
and compiler switches), it's not the case that 'char' is equivalent to
either 'signed char' or 'unsigned char' wrt. the type system.

Those are three distinct types in C++.

Meaning e.g. that you can overload a function like

void foo( char );
void foo( unsigned char );
void foo( signed char );

and which one is called for foo(c) depends on the type of c.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.