我在C++ quiz過這個問題(初學C++):我的答案是不正確的,我想知道正確的答案背後的解釋 - 「未定義行爲」這個C++測驗答案背後的解釋是什麼?
問: 後會發生下面的代碼是什麼函數foo()返回?
class base
{
public:
base() { }
~base() { }
};
class derived : public base
{
private:
int *p_pi_values;
public:
derived() : p_pi_values(new int[100]) { }
~derived() { delete [] p_pi_values; }
};
void foo(void)
{
derived *p_derived = new derived();
base *p_base = p_derived;
// Do some other stuff here.
delete p_base;
}
我給了這個答案,結果出錯==>整數數組將不會被正確刪除。
Correct Answer ==>行爲是未定義的。
這可能有助於http://stackoverflow.com/questions/5392590/destructor-in-c – silvesthu 2012-08-09 22:02:17