error or not ?

This is a discussion on error or not ? within the C++ Programming forums, part of the General Programming Boards category; Hi, all~ please look at this: Code: string s='!'; one of my book said it would cause an error when ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    error or not ?

    Hi, all~

    please look at this:

    Code:
    string s='!';
    one of my book said it would cause an error when compiling, but my compiler do accept it and pring a "!" as string in screen. is it relative wth compilers ?
    Never end on learning~

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>string s="!";
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    It seems you've answered your own question. The book says it'll throw an error but when you tested it, it didn't - therefore its probably a compiler specific issue. I would stick to the double quotes though, as demonstrated rather nicely by Hammer
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I think the book is wrong.

    Look -- the string class has a char assignment operator:

    basic_string& operator=(charT);

    Therefore, string s = '!'; is perfectly valid.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Davros
    I think the book is wrong.

    Look -- the string class has a char assignment operator:

    basic_string& operator=(charT);

    Therefore, string s = '!'; is perfectly valid.
    Check out this, the constructors for the string class. There isn't one where it takes a single char as an arg.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    To follow on from Hammer....

    Remember that string foobar = "Hello World" uses the copy constructor....not the assignment operator...If you want, you can

    Code:
    string foobar;
    	
    foobar = 'h';

  7. #7
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    OK, I take your point. There doesn't seem to be a char constructor.

    So we are saying that:

    string s = '!';

    is invalid, but

    string s;
    s = '!';

    is valid.

    It seems a little strang that we have an assignment operator, but not a char copy constructor. Is there any reason for this? May the copy constructor is implemented but not documented.

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    An error on my part.....I dont think it was a copy contructor...

    More likely the basic_string(const T*) constructor.......

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    I was always with Dev-C++ so I want to know it this format is valid in VC or BCB ?
    Never end on learning~

  10. #10
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Hi,
    I was wondering why
    string s = '!';
    isn't valid while
    string s;
    s='!';
    is valid?

  11. #11
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    string s;
    s = '!';

    is valid because string (basic_string) has an assignment operator which accepts a single char. Which looks like this:

    basic_string& operator=(charT);

    However, the string class does not have a constructor which accepts a single char. When we write :

    string s = '!';

    it would be the constructor which is called in this case, not the assignment operator.

    While it seems an oversight not to include a constructor, it's not really a big issue, because we can still write:

    string s = "!";

    In this case, "!" is char string array, i.e. a pointer to a char string (char*), rather than just a single char itself. Notice the double quote, instead of single.

  12. #12
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Thanks that's great

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21