Stroustrup 5.9 exercise 6 (char& as argument)

Discussion in 'C++' started by arnuld, Nov 8, 2006.

  1. arnuld

    arnuld Guest

    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?
    arnuld, Nov 8, 2006
    #1
    1. Advertising

  2. arnuld

    arnuld Guest

    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'"
    arnuld, Nov 8, 2006
    #2
    1. Advertising

  3. arnuld

    BobR Guest

    arnuld wrote in message
    <>...
    >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
    BobR, Nov 8, 2006
    #3
  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".


    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
    Victor Bazarov, Nov 8, 2006
    #4
  5. arnuld

    arnuld Guest

    > 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...
    arnuld, Nov 8, 2006
    #5
  6. * 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?
    Alf P. Steinbach, Nov 8, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arnuld

    Stroustrup 5.9 exercise 6

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    7
    Views:
    359
    arnuld
    Nov 8, 2006
  2. arnuld
    Replies:
    2
    Views:
    285
    arnuld
    Nov 8, 2006
  3. arnuld
    Replies:
    6
    Views:
    373
    arnuld
    Nov 8, 2006
  4. arnuld

    Stroustrup 5.9 exercise 9

    arnuld, Nov 8, 2006, in forum: C++
    Replies:
    5
    Views:
    369
    Bernd Strieder
    Nov 9, 2006
  5. arnuld
    Replies:
    0
    Views:
    506
    arnuld
    Mar 5, 2007
Loading...

Share This Page