I saw a sentence in The C++ programing language which I'm confused with:
• If the programmer declares a copy operation, a move operation, or a destructor for a class,no copy operation, move operation, or destructor is generated for that class.
I wrote a test code shown below:
#include <iostream>
using namespace std;
class A
{
public:
A() :a(0){}
A(int _a) :a(_a){}
int get() const
{
return a;
}
/*A& operator=(const A &x)
{
a = x.a;
cout << "A copy assignment" << endl;
return *this;
}*/
A& operator=(A&&x)
{
cout << "A move assignment" << endl;
swap(a, x.a);
return *this;
}
private:
int a;
};
A operator+(const A &x, const A &y)
{
A temp{ x.get() + y.get() };
return temp;
}
int main()
{
A a1(1), a2(2), a3;
a3 = a1;
cout << a3.get() << endl;
return 0;
}
I define a move assignment, there should be not default copy assignment generated as said in the book, but how could a3 gets the copy of a1?
Another question:
I modified a3 assignment expression:
a3 = a1+a2;
Then I comment out the move assignment and remove comment on copy assignment:
A& operator=(const A &x)
{
a = x.a;
cout << "A copy assignment" << endl;
return *this;
}
/*
A& operator=(A&&x)
{
cout << "A move assignment" << endl;
swap(a, x.a);
return *this;
}*/
how could copy assignment be called? The result of a1+a2 is a rvalue, how could this rvalue be passed to copy assignment whose argument is const A&? Forgive my confusion about rvalue
any help is appreciated!