6 考核点
析构函数的调用顺序。
7 试题
class A
{
public:
A* m_pDelegation;
public:
A()
{
printf("Call A constructor\n");
m_pDelegation = NULL;
}
virtual ~A()
{
printf("Call A destructor\n");
if(NULL != m_pDelegation)
{
delete m_pDelegation;
m_pDelegation = NULL;
}
}
};
class BasedA :public A
{
public:
BasedA()
{
printf("Call BasedA constructor\n");
m_pDelegation = this;
}
virtual ~BasedA()
{
printf("Call BasedA destructor\n");
}
};
int main(int argc, char* argv[])
{
A* pA = new BasedA();
delete pA;
return 0;
}
请问:上述这段程序的执行结果为:(D)
A:
Call A constructor
Call BasedA constructor
Call BasedA destructor
Call A destructor
B:
Call BasedA constructor
Call A constructor
Call BasedA destructor
Call A destructor
C:
Call A constructor
Call BasedA constructor
Call A destructor
Call BasedA destructor
D:
Call A constructor
Call BasedA constructor
Call BasedA destructor
Call A destructor
Call A destructor
....
最终程序异常退出。
阅读(413) | 评论(0) | 转发(0) |