#include <iostream>
using namespace std;
struct Item
{
Item () {cout << "Item constructor called." << endl;}
~Item () {cout << "Item destructor called." << endl;}
Item (const Item& item): x(item.x) {cout << "Item copy constructor called." << endl;}
Item (Item&& item) : x(std::move(item.x)) {cout << "Item move constructor called." << endl;}
Item& operator=(const Item& item) { x= item.x; cout << "Item assignment operator called." << endl; return *this;}
Item& operator=(Item&& item) { x= std::move(item.x); cout << "Item move assignment operator called." << endl; return *this;}
int x = 0;
};
struct ItemHandler
{
Item getItem()
{
cout << "getItem called."<< endl;
return item;
}
Item item{};
};
int main()
{
ItemHandler ih;
cout << "trying move assignment" << endl;
Item&& it = ih.getItem();
}
I was expecting that since ih.getItem() will create a copy then move assign it to it. But Here is the output that I got:
Item constructor called.
trying move assignment
getItem called.
Item copy constructor called.
Item destructor called.
Item destructor called.