2

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?

abhi4eternity
  • 448
  • 3
  • 8
  • 19
  • 2
    possible duplicate of [Isn't return value optimization (RVO) a bug?](http://stackoverflow.com/questions/3905869/isnt-return-value-optimization-rvo-a-bug) – iammilind Aug 13 '12 at 05:14
  • poor class name. you may call it `MyClass`, `TestClass` or some other names, but not `abc`, especially when your variable name is `a` `b` and `c`. – abcdabcd987 Aug 23 '12 at 14:36

3 Answers3

1

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.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • I presume you are talking about "elision of copy operation". I was surprised because it was used in the return by value case but not in the the return by reference case. Thanks for clarifying the same. – abhi4eternity Aug 13 '12 at 06:01
0

Its an optimization explicitly allowed by the standard.

In n3376
12.8 paragraph 31:

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects.

This optimization can be achieved via RVO (this case) and under certain conditions NRVO.

Martin York
  • 257,169
  • 86
  • 333
  • 562
0

You can disable optimization options on your compiler. Please check for the optimization flag used while compiling your code.

thanga
  • 163
  • 2
  • 13