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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:39:30+00:00 2026-06-06T15:39:30+00:00

I tested the behavior of copy constructor with functions that return an object by

  • 0

I tested the behavior of copy constructor with functions that return an object by value, and I came across a case where copy constructor does get invoked and a case it doesn’t.

Please consider the following code:

class A {
public:
    A() {}
    A(const A& a) {
        cout << "Copy CTOR: " << "This address is " << this 
             << " input address is "<< &a <<       "\n";
    }
};

A returnMyself(A& a) {
    cout<<"Myself address is: "<< &a << "\n";
    return a;
}

A returnLocal(A& a) {
    A local;
    cout<<"local address in returnLocal is  "<< &local << "\n";
    return local;
}

int main () {
    A a;
    cout<<"Before returnMyself\n";
    returnMyself(a);
    cout<<"After returnMyself\n\n";

    cout<<"Before returnLocal\n";
    returnLocal(a);
    cout<<"After returnLocal\n";
}

The output of main is:

Before returnMyself.
Myself address is: 0x7fff6afd88f0.
Copy CTOR Invoked: This address is 0x7fff6afd88d8. Input address is 0x7fff6afd88f0.
After returnMyself.

Before returnLocal.
Local address in returnLocal is  0x7fff6afd88d0.
After returnLocal.

As you can see, when I declare a local object and return it, copy constructor doesn’t get invoked, as opposed to returning a given reference-object which does invoke copy constructor.

Does anyone have an explanation for this? In general, what are the cases of invoking copy constructor from a function that returns-by-value?

Thanks!

  • 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-06T15:39:32+00:00Added an answer on June 6, 2026 at 3:39 pm

    This is copy elision. The compiler is clever enough to optimize that copy away.

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

Sidebar

Related Questions

I've tested so many times and get strange behavior.Lwuit version 1.5. I've created few
I was writing a pluginable function when I noticed the following behavior (tested in
i tested HttpResponse#flushBuffer and PrintWriter#flush on Tomcat 7 below, but it seemed that the
I'm seeing some different behavior between g++ and msvc around value initializing non-copyable objects.
I just had some weird behavior from a version of g++ for Windows that
It is widely known that adding declarations/definitions to namespace std results in undefined behavior.
Given that: 1) The C++03 standard does not address the existence of threads in
I noticed a very curious behavior that, if standard, I would be very happy
The following program, was compiled and tested, it sometimes return the result, and sometimes
Looking for a robot simulator that's multi-agent, light weight, behavior driven, and scriptible, visual

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.