简体   繁体   中英

Copy Constructor not being called when returning by value : C++

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 ie 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
    .....
    };

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM