构造函数的调用顺序是从父类到子类;而析构函数则是从子类到父类,不信的话请编译并运行下面的代码:
#include <iostream>
using namespace std;
class TestClass
{
public:
TestClass() { cout << "The TestClass object has been constructed." << endl; }
~TestClass() { cout << "The TestClass object has been destructed." << endl; }
};
class SubClass : public TestClass
{
public:
SubClass() { cout << "The SubClass object has been constructed." << endl; }
~SubClass() { cout << "The SubClass object has been destructed." << endl; }
};
int main()
{
SubClass* ob1 = new SubClass;
delete ob1; // destruct this object
system( "pause" );
return 0;
}