is it fine to assign zero(0) to a string object, in any case?

This is a discussion on is it fine to assign zero(0) to a string object, in any case? within the C++ Programming forums, part of the General Programming Boards category; I have seen wide use of the following type of String _name; _name = 0; in the book inside c++ ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    is it fine to assign zero(0) to a string object, in any case?

    I have seen wide use of the following type of

    String _name;
    _name = 0;
    in the book inside c++ object model (By Lippman).

    though my compiler VC++ 8 does not allow the code to compile
    and it requires to make it like

    name = '\0' or any other character or string. Is is possible to assign 0 to string object. OR that was an error in the book.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    It is not possible to assign 0 to a string object, since it is an integer, not a string.
    Also note that it is called "string", not "String", as C++ is case sensitive.
    Furthermore, take care not to begin names with _, since they are usually reserved for language implementations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24
    Thanks Elysia,

    Especially the extra thing, you made me know that " _" is used for language implementation.

    And would you pls, let me know where can i find more precise technical info about "how java is 2 step back and 1 step forward" mentioned in the quote of ur post.

    Warm Regards,
    Alex

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    However, the following example compiles:

    Code:
    #include <string>
    
    int main()
    {
        std::string s = 0;
    
    }
    It is equivalent to

    Code:
    std::string s((char*)0);
    but the result, I'm afraid, is undefined (GCC throws an exception).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by Alexpo View Post
    And would you pls, let me know where can i find more precise technical info about "how java is 2 step back and 1 step forward" mentioned in the quote of ur post.
    It is more of an opinion than actual fact. I know of maybe a few things in Java that is worse than C++ (again, opinion). No real technical facts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Quote Originally Posted by Alexpo View Post
    Thanks Elysia,

    Especially the extra thing, you made me know that " _" is used for language implementation.
    Underscores are fine. Names beginning with two underscores are reserved.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Underscores are not particularly fine, either, as they are discouraged on the forum.
    Use underscores for library implementations and such (not two underscores), and not in normal code. I believe that is the suggested advice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    >> Underscores are not particularly fine, either, as they are discouraged on the forum.

    That's not a big deal and it doesn't mean they are not fine.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24
    However, the following example compiles:

    Code:
    #include <string>
    
    int main()
    {
        std::string s = 0;
    
    }

    Yes, the code compiles, but doesn't execute.Throws exception.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The rule on underscores is:

    No double underscores anywhere in the name. No single underscore followed by a capital letter at the start of a name. No single underscore to start the name if it is in the global namespace.

    Since it is obviously difficult for people to remember the exact rules, a simpler version is no double underscores and no single underscores to start the name. That way you'll be safe.


    As to assigning 0 to a string or initializing it with 0, it's wrong. The standard states that when initializing a string with a char* pointer, the pointer shall not be null. So if you do initialize it with 0, you will get undefined behavior.

    Note that it is possible that in the Lippman book you refer to, he is using a different string class than the standard string class (which might be why it is referred to as String instead of string). If that's the case, then that String class might allow 0 to be passed to its constructor, but there's no way to know without looking at the book.
    Last edited by Daved; 09-22-2009 at 11:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 06:56 AM

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