Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 析构函数阶_C++_Class_Destructor - Fatal编程技术网

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;
    }