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 8695023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:55:46+00:00 2026-06-13T00:55:46+00:00

I have a rather basic C++ question: Consider a function that takes some input

  • 0

I have a rather basic C++ question: Consider a function that takes some input parameters and creates a std::string from those parameters like the one below:

std::string constructString( int some_parameter ) {
    
    std::stringstream ss;
    
    // Construct a string (arbitrarily complex)
    ss << "Some parameter is " << some_parameter << " right now";

    return ss.str();    //Am I not returning a temporary object here?
}

I understand that the stringstream object will go out of scope when the function returns, but doesn’t that invalidate the constructed string as well?

What would happen if I changed the return type to const char * and returned ss.str().c_str() instead?

Code like the above seems to work, but I suspect that’s just because the memory containing the ‘temporary’ object has not yet been overwritten with something else when I use it?

I have to admit, I’m rather confused in such situations in general; I’d appreciate it if someone could explain this whole "temporary objects" thing to me (or just point me in the right direction).

  • 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-06-13T00:55:47+00:00Added an answer on June 13, 2026 at 12:55 am

    You are returning a temporary object, but because you return it by value, the copy is created. If you return pointer or reference to temporary object, that would be a mistake.

    If you change the return type to const char * and return ss.str().c_str() you would return pointer to some buffer of temporary std::string returned by ss.str() and that would be bad.

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

Sidebar

Related Questions

this is a rather basic java question I have an array containing String that
Sorry for this rather basic question, but I have to get some sort of
I'm relatively new to SVN and have a rather basic question. I have several
I have some rather odd behavior in my D program that I've narrowed down
I have a rather basic and general question about atomicity of UPDATE ... SET
I have a rather basic question. I have several(5) UIImageView images on my screen.
Beware, this is a rather basic question (I think). I have a UserControl, called
I have a rather basic question about interfaces, something I'm rather new too. I
I'm learning jQuery/javascript and have a rather basic question. Why doesn't this work? Thanks
I have a very basic question: Why did Twitter choose Scala rather than Groovy

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.