[英]What exactly happens when a character is appended to a string literal with the '+' operator in C++?
Based on my reading, the following code: 根据我的阅读,以下代码:
string aggregate = "give" + 'n';
Should produce a resulting string with the value: 应该生成具有值的结果字符串:
"given". “给予”。
It instead produces garbage. 它反而产生垃圾。 Why doesn't the following happen? 为什么不发生以下情况?
"give" is converted to a std::string via the constructor that takes a pointer to a character array. “give”通过构造函数转换为std :: string,该构造函数获取指向字符数组的指针。
The '+' overload that takes a std::string and a character is called, returning a new string. 调用带有std :: string和字符的'+'重载,返回一个新字符串。
I am basing my theory on this man page. 我的理论基础在这个手册页上。
Now, I have heard that the first argument to an overloaded operator isn't a candidate for constructor conversion if the operator is a member of a class. 现在,我听说如果运算符是类的成员,则重载运算符的第一个参数不是构造函数转换的候选者。 I believe I read that in Koenig and Moo. 我相信我在Koenig和Moo读过这篇文章。 But, in this case I understand the '+' operator to be a non-member overload. 但是,在这种情况下,我理解'+'运算符是非成员重载。
I realize this seems like a ridiculous over-complication, but I like to know FOR SURE what is happening when I write code. 我知道这似乎是一个可笑的过度复杂,但我想肯定知道,当我写的代码发生了什么。
The expression "give" + 'n'
is evaluated first, adding (int)'n'
to the address of the string constant "give"
. 首先计算表达式"give" + 'n'
,将(int)'n'
到字符串常量"give"
的地址。 The result of this pointer arithmatic is assigned to the string object. 该指针算术的结果被赋予字符串对象。 No error is raised because the result is of type const char*
. 不会引发错误,因为结果是const char*
类型。
You should get used to thinking of everything on the right of the =
as happening before the actual assignment (or initialization in this case). 您应该习惯于在实际赋值之前(或者在这种情况下初始化)中考虑=
右边的所有内容。
You want the following: 您需要以下内容:
// construct the string object first
std::string aggregate = "give";
// then append the character
aggregate += 'n';
or, explicitly build the a string object first: 或者,首先显式构建一个字符串对象:
std::string aggregate = std::string("give") + 'n';
The fact that there is a std::string
on the left side of the expression doesn't matter for the first step: "give"
is a const char[5]
which decays to a const char*
and 'n'
is a char
, that is, an integer type. 表达式左侧有一个std::string
的事实对于第一步无关紧要: "give"
是一个const char[5]
,它衰变为一个const char*
而'n'
是一个char
,即整数类型。 Adding an integer to a pointer simply adds to the pointer's address. 向指针添加整数只会添加到指针的地址。 The result is again of type const char*
, which is then implicitly converted to std::string
. 结果再次是const char*
类型,然后隐式转换为std::string
。
Basically, your code is equivalent to: 基本上,您的代码相当于:
const char* c = "a string" + 'n';
std::string s = c;
So, to achieve the desired effect, try 所以,为了达到预期的效果,试试吧
std::string s = std::string("a string") + 'n';
You're adding the integer 'n' to the address of the literal "give". 您将整数'n'添加到文字“给”的地址。
Try it with: 尝试使用:
string aggregate = "long string long string long string long string long string long string long string " + 'A';
That should illustrate what's happening. 这应该说明发生了什么。
That code doesn't do what you think it does. 该代码不符合您的想法。
It takes the string literal "give"
which then effectively degrades into a const char[5]
, and then add the integer value of 'n'
to that pointer. 它需要字符串文字"give"
然后有效地降级为const char[5]
,然后将'n'
的整数值添加到该指针。 It then takes that new garbage pointer and tries to make a string out of it. 然后它接受新的垃圾指针并尝试从中生成一个字符串。
You want string aggregate = std::string("give") + 'n';
你想要string aggregate = std::string("give") + 'n';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.