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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:34:43+00:00 2026-06-07T12:34:43+00:00

From the code below below I was expecting for class CA that the following

  • 0

From the code below below I was expecting for class CA that the following would be called

  1. constructor creating the temporary object to be returned by the function foo
  2. copy constructor to create the variable to be passed to the variable a in the main
  3. another copy constructor that would create the a variable from the value returned by the function.

Why it is not the case? The result I have is just

A

While I was expecting

ABB

So only the constructor is called. Is compiler optimizing something behind the scenes or have I missed some C++ concept?

class CA{
public:
   CA(){ std::cout << "A"; }
   CA( const CA& ){ std::cout << "B"; }
   CA& operator=(const CA& ){ std::cout << "C";return *this; }
};

CA foo(){
      return CA();
}

int main(){
 CA a = foo();    
}
  • 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-07T12:34:46+00:00Added an answer on June 7, 2026 at 12:34 pm

    Is compiler optimizing something behind the scenes or I missed some C++ concept?

    Spot on! It’s called copy elision. Look up RVO and NRVO on google. You should also look up the rule of three.

    Copy elision is the only optimization the compiler is allowed to perform that affects observable behavior. You shouldn’t place important logic in the copy constructor for that reason.

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

Sidebar

Related Questions

What i want is that each animation, from the code below, to start with
From the disassembly code below can I assume that the location 43E010 is a
I have the code below: from the getData function im trying to call get_xml
I am creating a random ID using the below code: from random import *
In the code below I call a function (it happens to be a constructor)
Below is selected code from one of 5 classes for this assignment. Each class
From the code below I am getting a cell value from the selected row
I am getting a Value cannot be null error from the code below: @Html.Label(material.ExtendedGroup)
I have some really funky code. As you can see from the code below
I have 2 files in the directory bar for testing. from the code below,

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.