Inheritance and Constructors

Started by
0 comments, last by jpetrie 15 years, 3 months ago
It is my understanding that if you have two classes, Base and Derived, and you create an object of the Derived type, then Base::Base is called before Derived::Derived. I was wondering, if Base has multiple constructors, do you have any control over which one is called? Or is it always the default? If the latter is true, then I guess a default constructor is required? Edit: ugh, nevermind I already knew the answer to this. It's been a long day.
Advertisement
Assuming C++, you may invoke a base class constructor like
Derived::Derived(): Base( ... ) {}

where ... represent the arguments the constructor. In this fashion you can call any of the accessible base class constructors. If you do not explicitly invoke the base class constructor, the default constructor for the base will be called instead. If that constructor is unavailable, the compiler will emit an error and you will need to make the constructor accessible or invoke a suitable parameterized base class constructor.

This topic is closed to new replies.

Advertisement