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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:45:09+00:00 2026-05-31T09:45:09+00:00

Possible Duplicate: Why copy constructor is not called in this case? In the following

  • 0

Possible Duplicate:
Why copy constructor is not called in this case?

In the following code, I constructed three variables, a1, a2 and a3.

There’s a example in C++ Primer p.476:

string empty_copy = string();//copy-initialization

Is there anyone can help me explain

1)why a1 and a2 are not constructed by copy constructor and

2)what’s the difference between initialization a2 in my code and empty_copy in the book?

Thanks so much!

#include<iostream>
using namespace std;
class A{
public:
    A(){}
    A(int v){}
    A(const A&x){
        cout<<"copy constructor"<<endl;
    }
};
A generateA(){
    return A(0);
}
int main(){
        cout<<"First:"<<endl;
        A a1=generateA();

        cout<<"Second:"<<endl;
        A a2=A(0);

        cout<<"Third:"<<endl;
        A a3=a1;
    return 0;
}

The out put is (under Visual Studio 2010 in Win7 and g++ in Ubuntu10.10):

First:
Second:
Third:
copy constructor
  • 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-05-31T09:45:11+00:00Added an answer on May 31, 2026 at 9:45 am

    This is copy elision due to Return value optimization.
    Compilers are allowed to optimze away generation of copies by applying such optimizations.

    A a1=generateA(); 
    A a2=A(0); 
    

    In the above both cases the compiler can eliminate the creation of a temporary object which is created to hold the return value.

    A a3=a1;
    

    Involves a already existing named object a1 which is used for construction of a3 this involves a copy constructor call which the compiler must make and cannot optimize.

    EDIT: To answer the Q in comment.

    You can tell the compiler to not apply this optimization by using the following options during compilation:

    For GCC:

    -fno-elide-constructors
    

    For MVSC:

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

Sidebar

Related Questions

Possible Duplicate: Why copy constructor is not called in this case? Consider the sample
Possible Duplicate: question about copy constructor if I have this snippet of the code
Possible Duplicate: Why ‘this’ is a pointer and not a reference? Is there any
Possible Duplicate: How does Copy-local work? I have following situation: there's a project named
Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class
This is not a duplicate of Implementing the copy constructor in terms of operator=
Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this :
Possible Duplicate: Why has the destructor been called only once? Given the code below,
Possible Duplicate: Warning: The Copy Bundle Resources build phase contains this target's Info.plist file
Possible Duplicate: What's the most reliable way to prohibit a copy constructor in C++?

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.