I am confused with followed concepts:
我对以下概念感到困惑:
string str="123";
Some books say that: using "=" is copy initialization,
有些书说:使用“=”是复制初始化,
but some articles say: string str="123"
is same as string str("123")
. There is no doubt str("123") is directly initialization.
但有些文章说:string str =“123”与string str(“123”)相同。毫无疑问,str(“123”)是直接初始化的。
So which style for string str="123";
?
那么字符串str =“123”的样式是什么?
How to judge which is copy initialization or directly initialization?
如何判断哪些是复制初始化还是直接初始化?
2 个解决方案
#1
4
It's simply a matter of grammar:
这只是一个语法问题:
-
T x = y;
is copy-initialization, andT x = y;是复制初始化,和
-
T x(y);
is direct-initialization.T x(y);是直接初始化。
This is true for any type T
. What happens exactly depends on what sort of type T
is. For primitive types (e.g. int
s), the two are exactly the same. For class-types (such as std::string
), the two are practically the same, though copy-initialization requires that a copy-constructor be accessible and non-explicit (though it will not actually be called in practice).
对于任何类型T都是如此。究竟发生了什么取决于T的类型。对于原始类型(例如整数),两者完全相同。对于类类型(例如std :: string),两者实际上是相同的,尽管复制初始化要求复制构造函数是可访问且非显式的(尽管实际上不会实际调用它)。
#2
0
Yes that is called copy initialization.
是的,称为复制初始化。
Instead of default constructing str and then constructing another string
from "123"
using string(const char*)
and then assigning the two strings, the compiler just construct a string using string(const char*) with "123".
而不是默认构造str然后使用字符串(const char *)从“123”构造另一个字符串然后分配两个字符串,编译器只使用字符串(const char *)和“123”构造一个字符串。
string str="123"
is same asstring str("123")
. There is no doubt str("123") is directly initialstring str =“123”与字符串str(“123”)相同。毫无疑问str(“123”)是直接初始的
However remember that is possible only if the corresponding constructor is not explicit.
但是请记住,只有相应的构造函数不明确时才可能。
#1
4
It's simply a matter of grammar:
这只是一个语法问题:
-
T x = y;
is copy-initialization, andT x = y;是复制初始化,和
-
T x(y);
is direct-initialization.T x(y);是直接初始化。
This is true for any type T
. What happens exactly depends on what sort of type T
is. For primitive types (e.g. int
s), the two are exactly the same. For class-types (such as std::string
), the two are practically the same, though copy-initialization requires that a copy-constructor be accessible and non-explicit (though it will not actually be called in practice).
对于任何类型T都是如此。究竟发生了什么取决于T的类型。对于原始类型(例如整数),两者完全相同。对于类类型(例如std :: string),两者实际上是相同的,尽管复制初始化要求复制构造函数是可访问且非显式的(尽管实际上不会实际调用它)。
#2
0
Yes that is called copy initialization.
是的,称为复制初始化。
Instead of default constructing str and then constructing another string
from "123"
using string(const char*)
and then assigning the two strings, the compiler just construct a string using string(const char*) with "123".
而不是默认构造str然后使用字符串(const char *)从“123”构造另一个字符串然后分配两个字符串,编译器只使用字符串(const char *)和“123”构造一个字符串。
string str="123"
is same asstring str("123")
. There is no doubt str("123") is directly initialstring str =“123”与字符串str(“123”)相同。毫无疑问str(“123”)是直接初始的
However remember that is possible only if the corresponding constructor is not explicit.
但是请记住,只有相应的构造函数不明确时才可能。