#include <iostream>
class MyClass
{
public:
MyClass() = default;
MyClass& operator=(const MyClass& other) { std::cout << "copy\n"; i = other.i; return *this; }
MyClass& operator=(MyClass&& other) noexcept { std::cout << "Move\n"; i = other.i; return *this; }
MyClass(const MyClass& other) { std::cout << "copy construct\n"; i = other.i; };
int i = 10;
};
MyClass function()
{
MyClass c{};
return c;
}
int main()
{
MyClass c1;
MyClass c2;
c1 = function();
}
Why does the line c1 = function(); call copy constructor and move assignment operator? Where exactly does copy construction happen? I can understand that the contents of local variable c gets moved since it's in a local scope and it's going to be destroyed anyway. But where does copy construction happen?
And if I change function to:
MyClass function()
{
return MyClass{};
}
It only calls move constructor.