我如何派生一个具有带一些参数的构造函数的类?

时间:2013-06-14 17:15:26

标签: c++

如何派生一个具有带一些参数的构造函数的类?

//The construction in base class:
BaseClass::BaseClass(int inArgument) :
    m_args (inArgument) // where m_args is a public/protected member of the base class
{

}
//The construction of derived class:
DerivedClass::DerivedClass(int inArgument) :
    m_args (inArgument) // where m_args is a public/protected member of the derived class
{

}
编译后

我得到:错误1错误C2512:'BaseClass':没有合适的默认构造函数

我是初学c ++程序员......

1 个答案:

答案 0 :(得分:8)

只需将参数转发给基类的构造函数:

DerivedClass::DerivedClass(int inArgument) : BaseClass(inArgument)
//                                         ^^^^^^^^^^^^^^^^^^^^^^^
{
}