vendredi 29 mai 2020

Why `copy constructor` is used while `move constructor` has been removed?

As the subject,the related code is:

#include <iostream>     

class ABC     
{  public:  
    ABC() 
    {
        std::cout<< "default construction" << std::endl;
    }

    ABC(const ABC& a) 
    {
        std::cout << "copy construction" << std::endl;
    } 

    ABC(const ABC&& a) 
    {
        std::cout << "move construction" << std::endl;
    }
};                         

int main()   
{  
   ABC c1 = ABC();  

   return 0;  
}

Output with -fno-elide-constructors -std=c++11

default construction
move construction

If i remove the move constructor above, then the output is:

default construction
copy construction

Why copy constructor is used while move constructor has been removed? As per some documentation,compiler provides a default move constructor.So why don't the compiler use the default move constructor?

I am a novice in C++.I would be grateful to have some help with this question.

Aucun commentaire:

Enregistrer un commentaire