samedi 16 avril 2022

copy constructor not getting following return by overloaded assignment operator

class alpha
{
    private:
        int data;
        
    public:
        alpha(){cout<<"no-arg Constructor invoked\n";}
        alpha(int d):data(d){cout<<"1-arg Constructor invoked\n";}
        void display(){cout<<data;}
        alpha  operator=(alpha& a1)
        {
            data=a1.data; 
            cout<<"Assignment operator invoked:\n"; return alpha(data);
        }
        alpha(const alpha& a){cout<<"copy constructor invoked\n"; data= a.data;}
        friend void func(alpha);
};  
int main()
{
   alpha a1;
   alpha a2(3); 
   alpha a3(a2);//copy constructor invoked
   alpha a4(a3=a2);//**copy constructor not invoked why?**
   return 0;
}

alpha a4(a3=a2) does that have to do something with rvalue as the overloaded assignment operator returns rvalue of type alpha, or what else am I missing?

Aucun commentaire:

Enregistrer un commentaire