16
내가 비주얼 스튜디오에서 다음 프로그램을 시도 2010 년C++ 소멸자의 행동
#include <iostream>
using namespace std;
class A {
public:
int p;
/*A(){
cout << "Constructor A" << endl;
}*/
~A(){
cout << "Destructor in A" << endl;
}
};
class D: public A
{
public:
/*D(){
cout << "Constructor D" << endl;
}*/
~D(){
cout << "Destructor in D" << endl;
}
};
int main()
{
D d = D();
cout << "Exiting main" << endl;
}
내가 가진 출력했다 -
Destructor in D
Destructor in A
Exiting main
Destructor in D
Destructor in A
내가 이해할 수없는 나는 왜 클래스 D의 소멸자와 A는 "Exiting main"명령문이 실행되기 전에 라고 불리고 있습니까?
나는 다른 일을 시도 - 그때 출력 I 예상되면서 위의 코드에서 클래스 D 생성자를 주석 처리 -Constructor D
Exiting main
Destructor in D
Destructor in A
내가 여기에 놓친 거지 뭐?
왜 두 번째 예제가 더 이상 소멸자 호출을 인쇄하지 않는지 설명하지 않습니다. – Xeo
어떻게해야하는지 알려줄 수 있습니다 (: –
) 컴파일러는 elution을 복사해도 호출이 여전히 인쇄되어야합니다. – Xeo