开发者

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?

  1. "give" is converted to a std::string via the constructor that takes a pointer to a character array.

  2. The '+' overload that takes a std::string and a character is called, returning a new 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. But, in this case I understand the '+' operator to be a non-member overload.

I realize this seems like a ridicu开发者_如何学JAVAlous 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". The result of this pointer arithmatic is assigned to the string object. No error is raised because the result is of type 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. 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.

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".

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. It then takes that new garbage pointer and tries to make a string out of it.

You want string aggregate = std::string("give") + 'n';

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜