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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:59:17+00:00 2026-06-09T19:59:17+00:00

When [abc e = a+b] is called, the copy constructor is not called. class

  • 0

When [abc e = a+b] is called, the copy constructor is not called.

class abc{
        int i;
        public:
        abc()
            {
            i = 10;
            cout<<"constructor"<<endl; 
        }
        abc(const abc &a)
        {   
            cout<<"copy constructor"<<endl;
            i = a.i; 
        }
        abc operator=(const abc &a)
        {
            cout<<"operator="<<endl;
            abc temp;
            temp.i = a.i;
            return temp;
        }
        abc operator+(abc& a)
        {   
            cout <<"Operator+ called"<<endl;
            abc temp;
            temp.i = i+a.i;
            return temp ;
        }
    };

    int main()
    {
        abc a,b;
        cout <<"----------------------------------------------"<<endl;
        a = b;
        cout <<"----------------------------------------------"<<endl;
        abc c = a;
        cout <<"-----------------------------------------------"<<endl;
        abc d(a);
        cout <<"-------------------------------------------"<<endl;
        **abc e = a+b;** 
    }

However if the overload operators methods are replaced with the following methods that return references to the object of class abc, copy constructor gets called.

abc& operator=(const abc &a)
    {
        cout<<"operator="<<endl;
        i = a.i;
        return *this;
    }
    abc& operator+(const abc& a)
    {   
        cout <<"Operator+ called"<<endl;
        i = i+a.i;
        return *this ;
    }

Can some one please explain why does this happen?

  • 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-09T19:59:19+00:00Added an answer on June 9, 2026 at 7:59 pm

    This is happening due to return value optimization.

    Since no costly memory-copying operations are being made if your constructor returns a reference, it works as you would expect, because no optimization is being made in this case. If it returns a value, the optimization kicks in and it works in a somewhat unexpected (for you) manner, but it is allowed by the standard.

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

Sidebar

Related Questions

Possible Duplicate: C# constructor execution order class Foo { public int abc; Foo() {
I can not work this out. I have a class library called 'ABC.Service'. In
I have a dll called Test.dll in which I have a class called ABC
Let's say I have a class called 'foo' in namespace abc... namespace abc {
string abc = 07:00 - 19:00 x = int.Parse(only first two characters) // should
*JS* document.getElementsByClassName('abc')[0].setAttribute('id', 'abe'); *html* <div class=abc></div> <div class=abc></div> How to make this script work
i have a flash video called abc.swf that i would like to play when
I've a file with some text (8/9 lines) called abc.txt I'm trying to replace
I'm really new to apache mod_rewrite module. I have a page called http://abc in
I have a method 'ABC' that does some stuff. The ABC method is called

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.