Now I pass the Duck object with v.push_back( std::move(Duck( fb )) ); |
Unless you implemented a move constructor (abiding by the rule of three), this does not solve the problem.
Further, since
Duck(fb) is a prvalue expression, the call to
std::move will never have any effect, and it can be removed.
Note that the subexpression
Duck(fb) still has the same effects, and therefore the program will still exhibit the same issue. To avoid creating that temporary object at all, you could write
v.emplace_back( fb ) instead. This would work
fine until the vector resizes itself!
An option is to implement proper value semantics for Duck via the
Rule of Three, so that the identity of individual Duck objects becomes unimportant.
The ideal choice is to store smart pointers instead. This places the burden of resource management onto a standard library class, obeys the Single Responsibility Principle, and saves you a significant amount of detailed work.