473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Stroustrup 5.9 exercise 6 (char& as argument)

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
5 2206
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

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
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
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Vinokur | last post by:
========================================== Windows 2000 Professional CYGWIN_NT-5.0 1.5.4(0.94/3/2) GNU g++ version 3.2 20020927 (prerelease) GNU objdump 2.14.90 20030901...
8
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
3
by: Marcin Kalicinski | last post by:
void f(const char *&text); Is this a const reference to char * or a reference to const char *? And how to write both of them? thank you, Marcin
10
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
3
by: Alberto Giménez | last post by:
Hello. I have a doubt about argument conversion when passing arguments to a function. I have a function like: struct packet *pkt_interest(const char **usrs); struct packet is defined elsewhere...
2
by: Luca Bart | last post by:
Dear all, I have a struct in C++: typedef struct{ char Command; short a; }PACKET; I have to send by UDP and C++ command is: UDP->SendBuffer((char*)&Data,sizeof(PACKET),sizeof(PACKET));
2
by: Zorro | last post by:
In header file istream there is an overload for every built-in type, except char. Is there a reason for this? GCC 3.3.4. is the compiler. Thanks.
3
by: gevadas | last post by:
sample program #include <iostream> #include <vector> using namespace std; int find(char*& value,char** arr,int size) { for(int i = 0;i < size;i++)
6
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.