简体   繁体   中英

String concatenation

Why it is possible to do

const string exclam = "!";
const string str = exclam + "Hello" + " world";

And not possible to do this:

const string exclam = "!";
const string str = "Hello" + " world" + exclam;

I know (although can't understand why) that it is not allowed to do:

const string str = "Hello" + " world" + "!";

as it will be interpreted like const char[6] + const char[6] + const char[1] , so from other side, why this is not allowed also, or why it uses char[] and not string .

The + operator is left-associative (evaluated left-to-right), so the leftmost + is evaluated first.

exclam is a std::string object that overloads operator+ so that both of the following perform concatenation:

exclam + "Hello"
"Hello" + exclam

Both of these return a std::string object containing the concatenated string.

However, if the first two thing being "added" are string literals, as in:

"Hello" + "World"

there is no class type object involved (there is no std::string here). The string literals are converted to pointers and there is no built-in operator+ for pointers.

It's because you are concatanating const char[6] + const char[6] , which is not allowed, as you said.

In C++, string literals (stuff between quotes) are interpreted as const char[] s.

You can concatenate a string with a const char[] (and vice-versa) because the + operator is overridden in string, but it can't be overridden for a basic type.

const string exclam = "!";    // Initialize a c++ string with an ansi c string
const string str = exclam + "Hello" + " world"; // Using the operator+ method of exclam

You can do it because the operator+ of exclam will return a new string containing "!Hello", on which you subsequently call the operator+ with " world" as parameter, so you get another string which, finally, gets assigned to str by means of the copy constructor.

On the other hand

const string str = "Hello" + " world" + exclam;

cannot be executed because "Hello" is just a const char * , which doesn't have a operator+ taking a const char * as parameter.

(New answer, this was not possible back in 2010)

You can now write

const string str = "Hello"s + " world"s + "!"s;
//                        ^           ^      ^

By adding that s after a string literal, you tell the compiler it's actually a std::string and not a const char[] . That means you can call members functions on it. Eg ("ABC"s).back() but also +

In addition to the answers that explain the reason for your observations, I post here how to get rid of the problem (you might have figured this out already).

Replace

const string str = "Hello" + " world" + exclam;

with

const string str = string("Hello") + " world" + exclam;

so you make sure the first operand is a string and not a const char[] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM