C++ strings

Discussion in 'C++' started by Marcel, Jan 18, 2006.

  1. Marcel

    Marcel Guest

    I am C++ beginner.

    When i start building very basic things like hello world programs i am very
    attired by the <string> library so i put all text things in strings. Is that
    good programmers practice or should i do something else?

    Marcel
     
    Marcel, Jan 18, 2006
    #1
    1. Advertising

  2. Marcel

    Guest

    Marcel wrote:
    > I am C++ beginner.
    >
    > When i start building very basic things like hello world programs i am very
    > attired by the <string> library


    I don't know what you mean by that.

    so i put all text things in strings. Is that
    > good programmers practice or should i do something else?


    That is usually good practice. There are conditions when using char*
    is better but it is usually while dealing with C++/C mixtures such as
    when using win32, a C API.
     
    , Jan 18, 2006
    #2
    1. Advertising

  3. Marcel wrote:
    > I am C++ beginner.
    >
    > When i start building very basic things like hello world programs i am very
    > attired by the <string> library so i put all text things in strings. Is that
    > good programmers practice or should i do something else?


    Hard to judge from one sentence. Sounds like you're going over the top,
    and using string literals is perfectly fine as long as you declare them
    arrays of const char, like so

    const char programmer[] = "Marcel";

    No need to declare it 'std::string', in most cases.

    V
     
    Victor Bazarov, Jan 18, 2006
    #3
  4. * Marcel:
    > I am C++ beginner.
    >
    > When i start building very basic things like hello world programs i am very
    > attired by the <string> library so i put all text things in strings. Is that
    > good programmers practice or should i do something else?


    It's generally good practice for beginner's code -- and also for
    experienced... ;-)

    That way you won't hurt yourself.

    However, there are some situations where it's ungood, mostly in code
    that's not likely to be written by a beginner. Here's an example: you
    need a text constant defined in one implementation file, used by some
    code in another implementation file. A raw character array constant is
    then initialized as part of what the standard calls "static
    initialization", which happens before any of your code is executed,
    whereas a std::string is an object with a constructor that's executed to
    do the initialization, and so it's part of the "dynamic initialization",
    which in certain situations just might happen _after_ the code using
    that constant is executed -- and bang.

    Hth.,

    - Alf

    --
    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, Jan 18, 2006
    #4
  5. Marcel

    Marcel Guest

    "Marcel" <> schreef in bericht
    news:60da2$43ce5529$d969db07$...
    >I am C++ beginner.
    >
    > When i start building very basic things like hello world programs i am
    > very attired by the <string> library so i put all text things in strings.
    > Is that good programmers practice or should i do something else?
    >
    > Marcel
    >


    Ok thanks a lot!!!

    Marcel
     
    Marcel, Jan 18, 2006
    #5
  6. Marcel

    Gavin Deane Guest

    Victor Bazarov wrote:
    > Marcel wrote:
    > > I am C++ beginner.
    > >
    > > When i start building very basic things like hello world programs i am very
    > > attired by the <string> library so i put all text things in strings. Is that
    > > good programmers practice or should i do something else?

    >
    > Hard to judge from one sentence. Sounds like you're going over the top,
    > and using string literals is perfectly fine as long as you declare them
    > arrays of const char, like so
    >
    > const char programmer[] = "Marcel";
    >
    > No need to declare it 'std::string', in most cases.


    Surely there's no harm in declaring it std::string instead of const
    char[].

    Once someone has mastered the simplest Hello World program, they'll
    move on to the next level. Very soon they will probably encounter
    things like reading strings from the user (e.g. "enter your name"),
    passing strings to functions, concatenating them, modifying them. A
    beginner (and indeed anyone else unless they have a good reason) should
    be using std::string for these.

    If your only interest in learning C++ is to write a Hello World
    program, there's nothing to choose between std::string and const
    char[]. If you want to learn a bit more about C++ than that then you'll
    benefit from initally using only one technique for representing strings
    and the best technique is std::string.

    Gavin Deane
     
    Gavin Deane, Jan 18, 2006
    #6
  7. Gavin Deane wrote:
    > [..some basic things named..]
    > If you want to learn a bit more about C++ than that then you'll
    > benefit from initally using only one technique for representing strings
    > and the best technique is std::string.


    I do not believe in purposely limiting the beginners to using some
    language features and not others without any explanation, and simply
    labeling them "the best technique". But it probably is already clear.

    V
     
    Victor Bazarov, Jan 18, 2006
    #7
  8. Marcel

    Gavin Deane Guest

    Victor Bazarov wrote:
    > Gavin Deane wrote:
    > > [..some basic things named..]
    > > If you want to learn a bit more about C++ than that then you'll
    > > benefit from initally using only one technique for representing strings
    > > and the best technique is std::string.

    >
    > I do not believe in purposely limiting the beginners to using some
    > language features and not others without any explanation, and simply
    > labeling them "the best technique". But it probably is already clear.


    When I said 'best' I meant that I believe, when a beginner is learning
    to use strings in simple contexts like concatenating, passing to
    functions, basic user input, they are better off learning to do that
    with std::string than char arrays.

    If they are going to use std::string throughout lessons 2 to 5 (the
    simple concepts I've suggested above) then why start them on char
    arrays in lesson 1 (Hello World)?

    Teaching only std::string at that stage is not hiding other language
    features for the sake of hiding them. It is staying consistent for the
    sake of avoiding the risk of introducing some unnecesary confusion.

    Gavin Deane
     
    Gavin Deane, Jan 18, 2006
    #8
    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. Kurt Krueckeberg
    Replies:
    2
    Views:
    716
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    386
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    498
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    574
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
Loading...

Share This Page