Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:24:00+00:00 2026-05-22T14:24:00+00:00

I am reading Accelerated C++ by Koenig. He writes that the new idea is

  • 0

I am reading Accelerated C++ by Koenig. He writes that “the new idea is that we can use + to concatenate a string and a string literal – or, for that matter, two strings (but not two string literals).

Fine, this makes sense I suppose. Now onto two separate exercises meant to illuminate this .

Are the following definitions valid?

const string hello = "Hello";

const string message = hello + ",world" + "!";

Now, I tried to execute the above and it worked! So I was happy.

Then I tried to do the next exercise;

const string exclam = "!";

const string message = "Hello" + ",world" + exclam;

This did not work. Now I understand it has something to do with the fact that you cannot concatenate two string literals, but I don’t understand the semantic difference between why I managed to get the first example to work (isn’t “,world” and “!” two string literals? Shouldn’t this not have worked?) but not the second.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:24:01+00:00Added an answer on May 22, 2026 at 2:24 pm
    const string message = "Hello" + ",world" + exclam;
    

    The + operator has left-to-right associativity, so the equivalent parenthesized expression is:

    const string message = (("Hello" + ",world") + exclam);
    

    As you can see, the two string literals "Hello" and ",world" are “added” first, hence the error.

    One of the first two strings being concatenated must be a std::string object:

    const string message = string("Hello") + ",world" + exclam;
    

    Alternatively, you can force the second + to be evaluated first by parenthesizing that part of the expression:

    const string message = "Hello" + (",world" + exclam);
    

    It makes sense that your first example (hello + ",world" + "!") works because the std::string (hello) is one of the arguments to the leftmost +. That + is evaluated, the result is a std::string object with the concatenated string, and that resulting std::string is then concatenated with the "!".


    As for why you can’t concatenate two string literals using +, it is because a string literal is just an array of characters (a const char [N] where N is the length of the string plus one, for the null terminator). When you use an array in most contexts, it is converted into a pointer to its initial element.

    So, when you try to do "Hello" + ",world", what you’re really trying to do is add two const char*s together, which isn’t possible (what would it mean to add two pointers together?) and if it was it wouldn’t do what you wanted it to do.


    Note that you can concatenate string literals by placing them next to each other; for example, the following two are equivalent:

    "Hello" ",world"
    "Hello,world"
    

    This is useful if you have a long string literal that you want to break up onto multiple lines. They have to be string literals, though: this won’t work with const char* pointers or const char[N] arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an a GPU accelerated program that requires the reading of an
reading the documentation for java org.w3c.dom.ls it seems as a Element only can be
Reading through some of the questions here, the general concensus seems to be that
Reading through the SendAsync , BeginAsync method illustrations of Socket s, I realized that
Reading Kohana's documentation, I found out that the main difference in 3.0 version is
i am reading Accelerated C# 2010. and have a few questions Question 1 Instances
I came across this code today whilst reading Accelerated GWT (Gupta) - page 151
Reading through the CKEditor documentation , I see that they have an option to
I've been reading Accelerated C++ and I have to say it's an interesting book.
Reading Chomsky hierarchy ... ... I know regexp can't parse type-2 grammars (context-free grammars),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.