2

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

//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
{

}

after compiling I get: Error 1 error C2512: 'BaseClass' : no appropriate default constructor available

I am a beginner c++ programmer...

David Toth
  • 85
  • 9
  • yeah, can be possible duplicate, sorry for that, but I didnt even know how to search for this problem... so they are so called "parameterized constructors". Thanks for the link Oli Charlesworth, and for the answer from Andy Prowl as well, it worked! – David Toth Jun 14 '13 at 17:22

1 Answers1

8

Just forward the argument to the constructor of the base class:

DerivedClass::DerivedClass(int inArgument) : BaseClass(inArgument)
//                                         ^^^^^^^^^^^^^^^^^^^^^^^
{
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Moreover, this way is strictly required if you do not have a default ctor in the base class. "You must initialize the following with an initializer list: base classes with no default constructors, reference data members, non-static const data members, or a class type which contains a constant data member". See [this link](http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr388.htm) for more information. – ash Jun 14 '13 at 17:27