My problem is to understand how exactly these two constructors work. I have this class:
class moveClass
{
int variabile;
public:
moveClass(){}
//move constructor
moveClass(moveClass && arg)
{
cout<<"Call move constructor"<<endl;
variabile = arg.variabile;
}
//copy constructor
moveClass(const moveClass & arg)
{
cout<<"Call copy constructor"<<endl;
variabile = arg.variabile;
}
};
From what i understand, when i instance a new object of this class, a constructor is called based on the type of the parameter.
The advantage of the move constructor is that when an rvalue is used to instance an object, that object isn't copied, but just moved.
1 moveClass a;
2 moveClass b = a;
3 moveClass c = std::move(a);
Considering this example, can i say that when i instance b, a is copied, then assigned to b?
In other words up until line 2 i will have 3 objects in memory: a, b and a_copy.
While line 3 will just create the c object and no new copy objects.
Basically at the end of these three lines i will have in memory 4 objects. Is this correct?
The code of the constructors is also the same, so i expect that the only difference is the type of the argument passed.