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

  • SEARCH
  • Home
  • 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 4045398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:25:19+00:00 2026-05-20T13:25:19+00:00

Based on my reading, the following code: string aggregate = give + ‘n’; Should

  • 0

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 ridiculous over-complication, but I like to know FOR SURE what is happening when I write code.

  • 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-20T13:25:20+00:00Added an answer on May 20, 2026 at 1:25 pm

    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';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code, based on reading cplusplus.com , I'm trying to test my
Consider the 2 following methods of reading a string from a file: NSString *path
Based on my reading and testing, with asynchronous sockets, the socket itself can be
I've been reading that M3G is something based on OpenGL ES...so can I work
I've been reading this post Creating a login form in CodeIgniter based on Ion
I've spent many many hours tonight reading up on implementing the event-based asynchronous pattern
I want to sample data based on a timestamp field. I am reading huge
I am using the following code to load my flash file, contact a php
Thank you for quack in pointing out the off-by-one! The following code is my
I am trying to understand how Boost memory mapped files work. The following code

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.