我剛纔讀Constructors and initialization of derived classes
而且我遇到了一個問題。代碼如下。
問題是我評論父親的默認構造函數時。將會出現錯誤。
但是,默認的構造函數不應該由編譯器自動創建?
謝謝你回答我的問題。當派生類構造函數初始化列表時,基類沒有默認構造函數
#include <iostream>
using namespace std;
class Father{
public:
int food;
Father(){} // father default constructor
Father(int f):food(f){
}
};
class Child: public Father{
public:
int beverage;
Child(int b):beverage(b){
}
};
int main(int argc, const char * argv[])
{
Child c(10);
return 0;
}