My understanding is that the destructor is called on the input after the call to the move constructor. I decided to test that in the code (see bottom), but get different results from what I expected. I get the same results using std::unique_ptr.
Expect:
1
2
bar constructed
foo destructed //due to move
3
i'm foo
4
[Error?]
Actual:
1
2
bar constructed
3
i'm foo
4
i'm foo
5
foo destructed
foo destructed
Code:
#include <iostream>
class Foo
{
public:
~Foo()
{
std::cout << "foo deconstructed" << std::endl;
}
void speak()
{
std::cout << "i'm foo" << std::endl;
}
};
class Bar
{
public:
Bar(Foo&& foo) : foo_(foo)
{
std::cout << "bar constructed" << std::endl;
}
Foo foo_;
};
int main()
{
std::cout << "1" << std::endl;
Foo foo;
std::cout << "2" << std::endl;
Bar bar(std::move(foo));
std::cout << "3" << std::endl;
bar.foo_.speak();
std::cout << "4" << std::endl;
foo.speak();
std::cout << "5" << std::endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire