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
有人可以分享两者背后的技术细节吗?