vendredi 29 mai 2020

Differences between `copy constructor` and `move constructor`.Could anybody make it clear by giving some simple examples [duplicate]

As the subject, I understand copy constructor and I am confused after studying move constructor.

As the code below, i think copy constructor is used to copy the temporary object returned from fun123() to obj1, but it uses move constructor indeed.

I am a novice in C++.I had been confused for a long time.I would be grateful to have some help with this question.

#include <iostream>  
using namespace std;

class ABC  
{  
public:   
    const char *a;  
    ABC()  
     { cout<<"Constructor"<<endl; }  
    ABC(const char *ptr)  
     { cout<<"Constructor"<<endl; }  
    ABC(ABC  &obj)  
     { cout<<"copy constructor"<<endl;}  
    //ABC(ABC&& obj)  
    //{ cout<<"Move constructor"<<endl; }  
    ~ABC()  
    { cout<<"Destructor"<<endl; }  
};

ABC fun123()  
{ ABC obj; return obj; }  


int main()  
{   
    ABC obj1=fun123();//NRVO   
    return 0;  
}

Aucun commentaire:

Enregistrer un commentaire