gpt4 book ai didi

c++ - C++初始化中的 "several values"是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:43:18 25 4
gpt4 key购买 nike

这是一道关于C++ Primer(5th edition) Chapter 3.2, Page 84,85的问题。

When we have a single initializer, we can use either the direct or copy form of initialization. When we initialize a variable from more than one value, such as in the initialization of s4, we must use the direct form of initialization:

string s4(10, 'c'); //s4 is "cccccccccc"
string s5 = "hiya"; // copy initialization
string s6("hiya"); // direct initialization
string s7(10, 'c'); // direct initialization; s7 is cccccccccc

When we want to use several values, we can indirectly use the copy form of initialization by explicitly creating a (temporary) object to copy:

string s8 = string(10, 'c'); // copy initialization; s8 is cccccccccc

多个值”是什么意思?上面的文字没有明确揭示它。而且我无法理解“single initializer”,它与“从多个值初始化一个变量”相反吗?

最佳答案

我认为它们的意思是当你想将多个参数传递给构造函数时,你不能在不将这些参数包装到一个临时参数的情况下使用复制初始化:

string one1 = "one";
string one2("one");
string one3 = string("one");

对比:

string many1(10, 'c');
string many2 = string(10, 'c');

一个参数有三种形式,多个参数只有两种。

关于c++ - C++初始化中的 "several values"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36968463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com