#include <iostream>
class Box{
public:
int x;
Box(){
x=0;
std::cout << "Constructor" << std::endl;
}
Box(const Box& other){
x = other.x;
std::cout << "Copy constructor" << std::endl;
}
Box(Box&& other){
x = other.x;
other.x = 0;
std::cout << "Move constructor" << std::endl;
}
Box& operator=(Box&& other) {
x = other.x;
other.x = 0;
std::cout << "Move assignment" << std::endl;
return *this;
}
Box& operator=(const Box &other){
x = other.x;
std::cout << "Copy assignment" << std::endl;
}
~Box(){
std::cout << "Destructor" << std::endl;
x=0;
}
};
Box send(Box b){
std::cout << "Sending" << std::endl;
return b;
}
int main(){
Box b1, b2;
b1 = send(b2);
return 0;
}
In the output:
Constructor
Constructor
Copy Constructor
Sending
Move Constructor
Move Assignment
Destructor
Destructor
Destructor
Destructor
I'm not too sure why a move constructor then assignment was used when doing b1 = send(b2)
.