简体   繁体   English

以下声明之间有什么区别?

[英]What is the difference between the following declarations?

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". 根据我的教科书,第一个语句执行的操作是“使用C字符串初始化构造函数”。 So does the first statement define a C string and the second statement define a C++ string? 那么,第一条语句是否定义了C字符串,第二条语句定义了C ++字符串? Also please explain the difference between a C string and a C++ string. 还请解释一下C字符串和C ++字符串之间的区别。

Both lines create a C++ std::string named str . 这两行都创建了一个名为str的C ++ std::string And both initialize them from a C string. 并且都从C字符串初始化它们。 The difference is, how they are initialized: 不同之处在于它们的初始化方式:

The first is direct initialization : 首先是直接初始化

string str("Hello World");

This calls the string(const char *) constructor. 这将调用string(const char *)构造函数。

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 ). 这需要string(const char *)构造函数是非explicit (对于先前的方法,该构造函数可以是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 . 在C ++ 17之前的版本中:首先,创建一个临时对象(使用string(const char *) ),然后调用copy(或move)构造函数初始化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. 发布C ++ 17:在这里,因为该标准保证复制省略 ,所以仅调用string(const char *)构造函数。 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. 因此,对于这种特殊情况,两个初始化之间的末尾没有真正的区别,因此,两个str字符串将变为相同。

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. 两条线限定类型的变量std::string命名str由的构造构成std::string采用一个char const*作为其参数。 There is no difference in those lines. 这些行没有区别。

[...] C string [...] C++ string [...]? [...] C字符串[...] C ++字符串[...]?

What is commonly called a C-string is nothing but a zero terminated array of char : 通常称为C字符串的不过是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. 但是std::string是C ++标准库的一个类,用于管理动态长度的字符串资源。

"Hello World" is the c-string (null terminated sequence of characters). “ Hello World”是C字符串(以零结尾的字符序列)。 string (or std::string as its complete name is) is a c++ string (not null terminated) in both cases. 在这两种情况下, string (或std::string的全名是)都是c ++字符串(不以null终止)。

Both lines call the same constructor that takes the c string and constructs a std::string . 这两行调用相同的构造函数,该构造函数采用c字符串并构造一个std::string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM