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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:07:42+00:00 2026-06-15T23:07:42+00:00

class myObj { myObj() {}; } myObj MakeObj() { return( myObj() ); } main()

  • 0
class myObj
{
   myObj() {};
}

myObj MakeObj()
{
    return( myObj() );
}

main()
{
   myObj o = MakeObj();
}

In the MakeObj function, the obj is returned by value. So in the assignment, the created object is copied. It seems there are two operations – one to create the object and assign its fields in MakeObj and another to copy the object on return.

Are one of these steps optimized out by compilers?
What is the fastest way to create a complex object and assign it?

  • 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-15T23:07:43+00:00Added an answer on June 15, 2026 at 11:07 pm

    Semantically speaking, in C++03, there are two copies made in the code i.e when it returns from MakeObj(), but the first copy is usually get optimized away by the compilers. That optimization is referred to as Return Value Optimization.

    In C++11, the returned object is actually moved (if available and accessible), not copied, which is optimized away by the compilers.

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

Sidebar

Related Questions

My code: myobj = new Object(); classes.testegy = Class.extend({ init: function (token) { console.log(test
Public MyObj as Object Public Function Test(t as Type) as Boolean Return TypeOf MyObj
I am writing a python function with the following: class myObj(object): def __init__(self, args):
Say I somehow got an object reference from an other class: Object myObj =
I have a List < MyObj > I have also overriden Equals(Object) in class
Let's say I have the following code static class ... { static object myobj
public class MyObj implements Serializable { private transient Map<String, Object> myHash = new HashMap<String,
Consider a simple POJO Java Object: class MyObj { String a, b; Integer c;
If is there 1 thread per Timer ? for example : class MyObj {
I was wondering if there is a way to return an array value from

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.