473,796 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2232
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************ **********@e3g2 000cwe.googlegr oups.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
2754
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 ========================================== We can see that the same assembly code is generated for * foo2 (char& )
8
2248
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
3
14517
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
293
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
3
1952
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 (it has no interest to show it here). With that "const" modifier I want to state that usrs pointer will not be changed inside the function. Then, from the main program, I have this:
2
1870
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
1684
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
20835
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
5592
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 normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just...
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7547
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.