0
string str("Hello World"); string str="Hello World"; 

I don't seem to understand the difference between the two. According to my textbook, the operation that the first statement performs is "Initialization constructor using C string". So does the first statement define a C string and the second statement define a C++ string? Also please explain the difference between a C string and a C++ string.

2
  • Initialization Commented Nov 16, 2018 at 8:05
  • I think that your question is too broad. Please remove the "explain the difference between a C string and a C++ string.". If you need this information, please ask a new question (but it is likely that this question has already been asked). Commented Nov 16, 2018 at 8:51

3 Answers 3

4

Both lines create a C++ std::string named str. And both initialize them from a C string. The difference is, how they are initialized:

The first is direct initialization:

string str("Hello World"); 

This calls the string(const char *) constructor.

The second is copy initialization:

string str = "Hello World"; 

This needs that the string(const char *) constructor is non-explicit (for the previous method, the constructor can be explicit). We have a little bit differing behavior, depending on the version of the standard:

  • pre C++17: first, a temporary object is created (with string(const char *)), and then the copy (or move) constructor is called to initialize str. So, the copy (or move) constructor needs to be available. The copy constructor phase can be elided (so the object will be created just like as the direct initialization case), but still, the copy (or move) constructor needs to be available. If it is not available, the code will not compile.
  • post C++17: here, because the standard guarantees copy elision, only the string(const char *) constructor is called. Copy (or move) constructor doesn't need to be available. If copy constructor is not available, the code still compiles.

So, for this particular case, there is no real difference in the end between the two initializations, and therefore, both str strings will become the same.

Sign up to request clarification or add additional context in comments.

Comments

3

Both lines define a variable of type std::string named str that is constructed by the constructor of std::string that takes a char const* as its argument. There is no difference in those lines.

[...] C string [...] C++ string [...]?

What is commonly called a C-string is nothing but a zero terminated array of char:

"foobar"; // an array of 7 const chars. 7 instead of 6 because it is 0-terminated. char foo[] = "foobar"; // an array of 7 chars initialized by the string given 

std::string however is a class of the C++ standard library that manages string resources of dynamic length.

8 Comments

So both the strings are C++ strings? And in both the cases the copy constructor of class string is called?
Both times you tell the compiler, that str should be of type string (std::string with its full name, i suppose). No, not the copy constructor (a copy-ctor takes one argument of type class_type const &). "Hello World" is of type char const[12] which decays into a char const * (a pointer to the first char of the array), so std::string::string(char const*) is called.
I'm sorry but I'm not being able to understand what you are saying as I'm not familiar with these advanced concepts. Could you please explain it in an easy-to-understand manner?
You know what a variable definition is?
So when int foo; defines an integer named foo, string bar; defines a string named bar. Ok?
|
1

"Hello World" is the c-string (null terminated sequence of characters). string (or std::string as its complete name is) is a c++ string (not null terminated) in both cases.

Both lines call the same constructor that takes the c string and constructs a std::string.

3 Comments

Since C++11 everything you get out of a std::string is 0-terminated. It shouldn't concern the user how the data is represented internally.
@Swordfish it is a user concern once you have a std::string that has a 0 in the middle
@Swordfish i didnt consider it either until one day i had a myserious bug and only after weeks I relaized what was the cause, made a good lesson ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.