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

How can I derive a class that has a constructor that takes some arguments?

本文关键字:参数 构造函数 何派生 派生      更新时间:2023-10-16

我如何派生一个类,有一个构造函数接受一些参数?

//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':没有适当的默认构造函数可用

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

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