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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:36:51+00:00 2026-06-04T21:36:51+00:00

I want to be sure I understand correctly. In the c++ functions below three

  • 0

I want to be sure I understand correctly. In the c++ functions below three instances of
std::string will be created:

  • one for s1 in caller() via what I believe is called the assignment constructor
  • one for the s2 parameter of the callee() function via it’s copy constructor
  • one for s3 via its copy constructor

Am I correct? And if so will all three instances be cleaned up as they go out of scope? I’m not really asking if this is good code or not, just if my understanding is correct.

void caller(void) {
    std::string s1 = "hi";
    callee(s1);
}

void callee(std::string s2) {
    std::string s3 = s2;
}
  • 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-04T21:36:54+00:00Added an answer on June 4, 2026 at 9:36 pm

    You are almost correct.

    Either three or four strings may be created (depending on whether the construction of s1 is elided), and in each case a constructor is called to construct them. Despite appearances, there are no calls to any assignment operators.

    void caller(void) {
        //A temporary std::string is constructed with the
        //basic_string(const CharT* s, const Allocator& alloc = Allocator())
        //constructor.
        //(In the call, `s` is initialized to point to the first element of "hi".)
        //This temporary is then move constructed in to s1.
        //The move constructor is
        //basic_string(basic_string&& other)
        //This move construction may be elided.
        std::string s1 = "hi"; //At the end of the full expression (ie, at the semicolon)
                               //the lifetime of the temporary string ends (unless
                               //the construction of s1 is elided, in which
                               //case the temporary becomes s1, and its lifetime ends
                               //with s1).
        //s2 is copy constructed from s1
        //The copy constructor is
        //basic_string(const basic_string& other)
        callee(s1);
        //the lifetime of s1 ends
    }
    
    void callee(std::string s2) {
        //s3 is copy constructed from s2
        std::string s3 = s2;
        //the lifetime of s3 ends
        //the lifetime of s2 ends
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure I understand correctly how QSound.setLoop function should work. I want to
I Just want to make sure I understand this correctly... search is an object
Just want to make sure I understand this correctly (I'd ask on SO Chat,
I want to make sure that I correctly understand the 'Effectively Immutable Objects' behavior
I want to make sure I understand a key concept of properties and instance
I want to make sure a string has only characters in this range [a-z]
Just want to make sure I understand things. From what I gather so far,
I want to make sure the HTML that I generate with the HtmlAgility pack
I want to make sure I'm not inserting a duplicate row into my table
I want to make sure the first 4 letters in a eight character 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.