C++ 析构函数阶
我想知道第一个输出幽灵来自哪里,以及为什么最后三个输出不是C++ 析构函数阶,c++,class,destructor,C++,Class,Destructor,我想知道第一个输出幽灵来自哪里,以及为什么最后三个输出不是 Ghost() Ghost(Inky) PacMan() Ghost(other) Ghost() PacMan(other) ~PacMan() ~Ghost() ~Ghost(other) ~Ghost(Inky) ~PacMan() ~Ghost() 我认为析构函数的顺序与构造函数的顺序相反,是真的吗?第一个幽灵是PacMan成员blinky 关于上一个订单:销毁pm1 Executes ~PacMan() ~Ghost(Ink
Ghost()
Ghost(Inky)
PacMan()
Ghost(other)
Ghost()
PacMan(other)
~PacMan()
~Ghost()
~Ghost(other)
~Ghost(Inky)
~PacMan()
~Ghost()
我认为析构函数的顺序与构造函数的顺序相反,是真的吗?第一个幽灵是PacMan成员blinky
关于上一个订单:销毁pm1 Executes
~PacMan()
~Ghost(Inky)
~Ghost()
然后blinky也被删除了。
如果你想要相反的顺序,你必须写在这里。解决这些问题的正确工具是你的调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该在问题中包含一个重现问题的示例,以及您在调试器中的观察结果。删除nullptr;这是合法的。如果p!=nullptr删除p;这是一种反模式。
~PacMan()
~Ghost(Inky)
~Ghost()
~PacMan(){
if (inky!= NULL)
delete inky;
cout <<"~PacMan()" << endl;
}