Stroustrup 5.9 exercise 6 (char& as argument)

A

arnuld

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?
 
A

arnuld

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'"
 
B

BobR

arnuld wrote in message
i wanted to tell that i got the answer to my 2nd question:


as /const char&/ does implicit conversion.

but still 1st question still exist:
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.".
 
V

Victor Bazarov

BobR said:
[..]
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
 
A

arnuld

BobR said:
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...
 
A

Alf P. Steinbach

* arnuld:
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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top