Consider this code:
#include <iostream>
using namespace std;
class A
{
int x;
public:
A () {x=5;}
A (const A & a) {x=a.x;}
A (A && a) {x=move(a.x);}
A & operator= (const A & a) {x=a.x;}
A & operator = (A && a) {x=move(a.x);}
void func () {A a; *this=move(a);}
};
int main () {A a; a.func();}
A::func()
creates an A object, then *this
is assigned to A using move operator=
. What are the differences between move operator=
and copy operator=
in that assignment?
Is it more efficient to use move assignment operator explicitly (using move
) rather than the copy operator when the object I want to copy will expire at the end of the function?
If I use the move assignment operator does a
still exist, after the assignment?