问题描述
这里,当我入栈时,为什么对象被销毁了?
#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";
}
我希望在 Now I'll stop
之前看不到任何输出。但我明白了
value 1 is destroyed
value 2 is destroyed
Now I'll stop
value 1 is destroyed
value 2 is destroyed
如果我想防止它们被破坏,我该怎么做?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)