【发布时间】:2016-02-26 09:45:47
【问题描述】:
我不明白为什么,当我尝试创建基类的对象时,我可以,但只能在不声明派生类的情况下,并且在声明 Derived 时我需要在基类构造函数中将参数值设置为“= 0”?
#include <cstdlib>
using namespace std;
class Base {
public:
int m_nValue;
Base(int nValue=0) //<--- why here i need = 0 ?
: m_nValue(nValue)
{}
};
class Derived: public Base {
public:
double m_dValue;
Derived(double dValue)
: m_dValue(dValue)
{}
};
int main(int argc, char** argv) {
Base cBase(5); // use Base(int) constructor
return 0;
}
【问题讨论】:
标签: c++ inheritance derived