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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:02:57+00:00 2026-06-12T05:02:57+00:00

I am coming from Java where anything is reference, so I am trying to

  • 0

I am coming from Java where anything is reference, so I am trying to figure out the basic of c++ instance creations.

Employee getEmp(int a) {
    Employee local(a);
    return local;
}
Employee myEmp = m.getEmp(10);

Is it correct that there are 3 times Employee instantiation (without RVO)?
1 – Create a local copy
2 – Temporary is created for binding to the reference)
3 – create myEmp

Why there is need in step 2? Why doesn’t copy the local directly to myEmp?

If I am going to use only Ubuntu should I count on RVO and am I correct that with RVO only two instance creation is done (no need in step 2)?

Thank you!!!

  • 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-12T05:02:58+00:00Added an answer on June 12, 2026 at 5:02 am

    The semantics for function return are defined independantly of what you
    do with the return value: the returned value is copied somewhere where
    it won’t disappear when the local stack frame is destructed. So without
    RVO, in your example, you have:

    1. Construction of a local variable, named local, in the stack frame
      of getEmp.

    2. Copy construction of the returned object somewhere which the caller
      will be able to access. Typically, the caller allocates memory for the
      returned object in its own stack frame, and passes a hidden argument
      with the address of this memory to the called function. RVO or NRVO
      allow the compiler to “alias” this returned object to a local object: in
      your case, to create the variable local at the address passed in as a
      hidden argument, rather than in the local stack frame.

    3. After return, the returned object will be “used”, then destructed at
      the end of the full expression. If this use is to initialize a local
      variable using the copy constructor, the intermediate value may be
      elided; the compiler will pass the address of the actual object to be
      constructed as hidden argument to the called function. (Strictly
      speaking, this is not NRVO, but it is related.) Other uses, however,
      cannot be elided; if the returned value is used in an assignment to an
      existing variable, for example, the assignment operator will require a
      reference to the instance to be assigned. The formal requirement for
      the intermediate object is there because most uses of return values are
      not as an argument to the copy constructor.

    Finally, given modern compiler technology, you can probably count on the
    compiler systematically eliding the second copy above. The first copy
    (RVO or NRVO) may be somewhat more difficult. I would expect to see it
    systematically in simple cases like the above: RVO anytime there is a
    single return of a temporary, and NRVO anytime there is a single return
    which returns a named variable defined at the outermost scope of the
    function. NRVO (and in practice RVO as well, although I don’t know why)
    is less likely to occur if there are several returns in the function.

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

Sidebar

Related Questions

I'm coming from Java/C++ to Ada and am having trouble figuring out the small
In some code we're trying out, coming from various tutorials, we end up missing
I just started learning C++(coming from Java & Python) and am trying to learn
Coming from Java I'm trying to learn thread safety in Objective-C. So far I've
Coming from Java and using the ArrayList class has me all frustrated when trying
I'm comming from the java world, and i'd like to know if there's anything
Coming from Java , I'm used to the package structure (com.domain.appname.tier) Now I've started
I am coming from Java to Ruby and this -7 mod 3 = 2
Background: I'm coming from Java background so not too familiar with Javascript. We are
new to Laravel (coming from Java spring), while using Route::get('/', function() { return Hello

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.