I have a piece of code
#include <iostream>
class A {
public:
A() {
std::cout << "Default constructor" << std::endl;
}
A(const A & other)
{
std::cout << "Copy constructor" << std::endl;
}
A(A && other)
{
std::cout << "Move constructor" << std::endl;
}
~A()
{
std::cout << "Destructor" << std::endl;
}
private:
int i;
};
A && f()
{
return A();
}
int main() {
A a = f();
}
I tried to run it, and the output is
Default constructor
Destructor
Move constructor
Destructor
My question is: why is the destructor called before the moved constructor? Does this mean that the second object was built with the value destroyed?
source
share