`
#include <iostream>
#include <vector>
class Move {
private:
int* data;
public:
Move(int d) {
data = new int;
*data = d;
std::cout << "Constructor is called for " << d << std::endl;
}
Move(const Move& source) : Move{*source.data} {
std::cout << "Copy constructor is called - " << "Deep copy for " << *source.data << std::endl;
}
Move(Move&& source) noexcept : data{ source.data } {
std::cout << "Move constructor for " << *source.data << std::endl;
source.data = nullptr;
}
~Move() {
if (data != nullptr) {
std::cout << "Destructor is called for " << *data << std::endl;
}
else {
std::cout << "Destructor is called for " << "nullptr" << std::endl;
}
delete data;
}
};
int main() {
std::vector<Move> vec;
vec.push_back(Move{ 10 });
vec.push_back(Move{ 20 });
return 0;
}
`
found this code example on the net, when i compile with one object inside vector one message from constructor one from move constructor and two messages from destructor are called, but when i compile with two objects two messages from constructor three messages from move constructor and five from destructor.
This really confuses me what happens. Could you please explain in details if possible.