【发布时间】:2012-02-16 10:26:24
【问题描述】:
请看下面的例子:
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; }
};
此代码不断向我抛出一个错误,即基类没有默认构造函数。当我宣布一切正常时。但是当我不这样做时,代码不起作用。
如何在派生类中声明复制构造函数而不在基类中声明默认构造函数?
感谢。
【问题讨论】:
-
一旦我们有任何参数化的构造函数,编译器就不会为类提供默认构造函数..
标签: c++ inheritance copy-constructor