在運行下面的代碼,爲何是基類的構造函數導出的第一即使我們首先聲明派生類的對象。爲什麼基類的構造函數首先調用?
#include<iostream>
using namespace std;
class base {
public:
base()
{ cout<<"Constructing base \n"; }
~base()
{ cout<<"Destructing base \n"; }
};
class derived: public base {
public:
derived()
{ cout<<"Constructing derived \n"; }
~derived()
{ cout<<"Destructing derived \n"; }
};
int main(void)
{
derived *d = new derived(); //d is defined ahead of the base class object
base *b = d;
delete b;
return 0;
}
你還會期待什麼?爲什麼? – user0042
提示:在'derived'裏面有一個'base' **子對象**。 – StoryTeller
問題不清楚。你可以解釋嗎? – MoraRockey