C++ strings

M

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?

Marcel
 
R

roberts.noah

Marcel said:
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.
 
V

Victor Bazarov

Marcel said:
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
 
A

Alf P. Steinbach

* 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
 
M

Marcel

Marcel said:
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
 
G

Gavin Deane

Victor said:
Marcel said:
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
 
V

Victor Bazarov

Gavin said:
[..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
 
G

Gavin Deane

Victor said:
Gavin said:
[..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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top