请看下面的例子:
class Base
{
protected:
int m_nValue;
public:
Base(int nValue)
: m_nValue(nValue)
{
}
const char* GetName() { return "Base"; }
int GetValue() { return m_nValue; }
};
class Derived: public Base
{
public:
Derived(int nValue)
: Base(nValue)
{
}
Derived( const Base &d ){
std::cout << "copy constructor\n";
}
const char* GetName() { return "Derived"; }
int GetValueDoubled() { return m_nValue * 2; }
};
这段代码一直抛出一个错误,说基类没有默认构造函数。当我声明它时,一切正常。但是当我没有声明时,代码就无法工作。
如何在派生类中声明复制构造函数而不声明基类的默认构造函数?
谢谢。