Here, when I push to the stack, why are the objects being destroyed?
#include <iostream>
#include <stack>
class One
{
private:
int i;
public:
One(int i) {this->i = i;}
~One() {std::cout << "value " << this->i << " is destroyed\n";}
};
int main()
{
std::stack<One> stack;
stack.push(One(1));
stack.push(One(2));
std::cout << "Now I'll stop\n";
}
I expected to see no output before Now I'll stop
. But I get this
value 1 is destroyed
value 2 is destroyed
Now I'll stop
value 1 is destroyed
value 2 is destroyed
What should I do if I want prevent them from destroying?