class Base
{
private:
int nID;
friend int fnDeleteBase(Base* base);
public:
Base(int baseID):nID(baseID) { cout << "Base Constructed with value" << endl; }
Base() : nID(5){cout << "Base Constructed WITHOUT value" << endl; }
~Base() { cout << "Base class object killed " << endl; }
};
int fnDeleteBase(Base* base) // Line 1
{
delete base; // Line 2 - important
cout << "Base object deleted " << endl;
return (1);
}
int main()
{
Base aBase; // Line 3
try
{
int i = fnDeleteBase(&aBase); // Line 4
}
catch(...)
{
cout << "Exception handled " << endl;
}
return (0);
}
上面的代碼片段我正在調試。我無法在第2行中刪除基礎對象。當我試着踏入或碾過線2時進入,我要殺死調試或執行我失去控制,無法調試
我得到的輸出是:
Base Constructed (any of the construction is valid)
Base class Object killed
然而一切工作正常,如果第3行更改爲Base * aBase = new Base();
。控制檯上的輸出是:
Base Constructed (any of the construction is valid)
Base class Object killed
Base object Deleted
有人可以分享兩者背後的技術細節嗎?
你在用什麼編程環境,Rajesh? –
visual studio ultimate 2010 –
順便說一句我首先嚐試fnDeleteBase參數作爲參考基地。結果與第一種情況相同。然後我試着用指針。 –