0
votes

Consider a Class:

class loc{
    int x;
    int y;
    public:
    loc();
    loc(int x,int y);
    loc(const loc& l);//Copy Constructor
    loc operator + (const loc& l);
    loc operator - (const loc& l);
    loc& operator = (const loc& l);//Assignment Operator
    const loc& operator ++ ();
    friend ostream& operator << (ostream& os,const loc& l);
    friend istream& operator >> (istream& is,loc& l);
    ~loc();
    };

When I invoke Assignment operator:

int main()
{
loc Ob;


cout << "########\n\n";
cin >> Ob;
cout << "Ob : " << Ob;

cout << "\n\n########\n\n";

loc Ob1;
Ob1=Ob;
cout << "Ob1 : " << Ob1;

   return 0;
}

It does not invoke copy constructor as it is returning by reference:

Output:

Constructor loc() Called
########

Enter x cordinate : 10
Enter y cordinate : 10
Ob :  x : 10 y : 10


########

Constructor loc() Called
Ob1 :  x : 10 y : 10

But for loc operator + (const loc& l) it should call copy constructor while returning temp -- as loc is being returned by value , but in output I can not see copy constructor being called:

loc loc::operator + (const loc& l)
{
    loc temp;
    temp.x = this->x + l.x;
    temp.y = this->y + l.y;
    return temp;
    }

Output:

int main()
{
    loc Ob;


    cout << "########\n\n";
    cin >> Ob;
    cout << "Ob : " << Ob;

    cout << "\n\n########\n\n";

    loc Ob1;
    Ob1=Ob;
    cout << "Ob1 : " << Ob1;

    cout << "\n\n########\n\n";

    loc Ob3;
    Ob3 = Ob1 + Ob;
    cout << "Ob3 = Ob1 + Ob : " << Ob3;

return 0;
}

Constructor loc() Called
########

Enter x cordinate : 10
Enter y cordinate : 10
Ob :  x : 10 y : 10


########

Constructor loc() Called
Ob1 :  x : 10 y : 10


########

Constructor loc() Called
Constructor loc() Called
Destructor Called
Ob3 = Ob1 + Ob :  x : 20 y : 20

Can Some One explain why this behavior?

EDIT Please note this is not case of copy Elision as if I return by loc i.e. instead of loc& in assignment operator - I can see copy constructor being called while returning loc in assignment operator as well as + operator. So this is something that in +operator while returning loc uses assignment operator . I want to know how a value is returned from a function, the intrinsic detail as how temp return is created and assigned the return value.

Below Class shows copy constructor being called while returning for loc operator + (const loc& l) as well as loc operator = (const loc& l)

class loc{
    ....
    loc(const loc& l);//Copy Constructor
    loc operator + (const loc& l);
    loc operator = (const loc& l);//Assignment Operator
    .....
    };
1
See RVO.juanchopanza
@juanchopanza It is not the case of copy elision, if I return by loc in assignment operator - I can see copy constructor being called while returning loc in assignment operator as well as + operatorGaurav K

1 Answers

6
votes

Compilers are allowed to skip out on calling the copy constructor in specific scenarios, and this is one of them, even though it produces a noticeable change (as you have just experienced). This is a common technique, known as Named Return Value Optimization (NRVO), where the returned variable is constructed in place instead of created and copied.