开发者

C++ Adding String Literal to Char Literal

I have a question about string concatenation in C++.

string str = "ab" + 'c';
cout << str << endl;

char ch = '开发者_运维百科c';
string str1 = "ab";
string str2 = str1 + ch;
cout << str2 << endl;

The code produces:

ed before SaveGraphicsState
abc

Can someone explain the processing of this line: string str = "ab" + 'c'; ?


Your thought regarding the first line is correct, that's precisely what's happening.

There isn't any default + operator for literal strings like "ab" so what happens is the compiler takes that (as a C-style string) and uses the const char* pointer that points to the literal. It then takes your literal character 'c' and promotes it to int with some value. This int is then added to the address of the literal and used as a C-string. Since you've exceeded the space allocated for your literal string, the results are undefined and it just printed out characters from the resulting address until it found a null.

If you want to create the string in one shot, you can help the compiler figure out that you wanted to convert to string first with a cast: std::string str = std::string("ab") + 'c';. Alternately (as seen in a separate comment) do it with concatenation which may or may not perform better. Use whichever seems clearer in your case: std::string str = "ab"; str += 'c';.

In the second case, you have already created a string and string has an overloaded operator+ that does the intuitive concatenation.


Your guess is correct, except the string literal isn't on stack, it's in a toolchain-specific location in memory, often in a read-only section.


string str = "ab" + 'c';

string literals cannot be concatenated like that. "ab" is an array of character, which decays into pointer (in this context) and you're adding 'c' which is an integral to the pointer. So the pointer is advancing by the ascii value of 'c'.

That is, the above code is equivalent to this:

char char * s= "ab";
string str = &s['c']; //the ascii value of 'c' acts like an index to the array. 

I'm sure that isn't what you intended. In fact, it invokes undefined behavior, because &s['c'] refers to a memory region which might not be in the process's address space.


The short form of what you actually want to do (i.e concatenation), is this:

string str = string("ab") + "c";


Operator overloading only works where at least one of the overloaded operator's parameters is a user-defined type (i.e. a class instance), so the + operator cannot be overloaded to add a character string and a character. and do something sensible. At best, you will get pointer arithmetic being performed - almost certainly not what you want. The usual workround for this is an explicit conversion:

string s = string( "foo" ) + "bar";    // s will contain "foobar"


"ab"

Is a C-string.

'c'

Is a character.

Try:

string str = string("ab") + "c";

If you want to make it simpler, there's always:

string str = "ab";
str += 'c';

Alternatively, you can use std::stringstream:

stringstream ss;
ss << "ab" << 'c';


"ab" is a const char *. 'c' is a char. My guess is that 'c' is converted to an integer, said integer is added to the address of "ab", and the resulting pointer is passed to std::string's constructor. You're lucky it's not miserably crashing.

See the other answers for how to do the concatenation properly.


I think string str = "ab" + 'c'; works something like:

string tmp = stirng("ab"); // implicit conversion
tmp = tmp + 'c'; // uses overloaded + operator
str = tmp;


This is build into C++:

#include <iostream>
#include <string>

int main()
{
    std::string s("Stand back! I've got jimmies!" "DDD");
    std::cout << "Stand back! I've got jimmies!" "DDD";
}

output:

Stand back! I've got jimmies!DDD
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜