Constructor and Destructor Inheritance
Solution 1
Your understanding is correct. For example, if you have
class Base
{
Base(int i) {}
};
class Derived: public Base {};
Derived d(3);
This will not compile because the Base constructor is not inherited. Note that default and copy constructor are created by the compiler if possible, and call the corresponding constructor of base classes, therefore for those constructors it looks as if those were inherited.
Solution 2
I think this is what you are looking for? You can call the superclass constructor by adding the following to your class constructor
SubClass(int foo, int bar)
: SuperClass(foo)
A full example can be found here What are the rules for calling the superclass constructor?
![nitin_cherian](https://i.stack.imgur.com/T0R9U.jpg?s=256&g=1)
nitin_cherian
nitin_cherian: Sneior Lead Engineer at ADVA Optical Networking, Bangalore, India Languages worked on: C, C++, Python Operating systems worked on: Linux Current Interests: Python Language, Game development in Python. Crafting Quality code in Python. Online Courses: Cryptography1, Interactive Programming in Python @ www.coursera.com. Hobbies: Reading short articles in newspaper and magazines.
Updated on June 09, 2022Comments
-
nitin_cherian about 2 years
I believe
Constructors
andDestructors
inbase class
cannot be inherited byderived classes
of the base class. Is my understanding correct.