Multiple Inheritance

Hello to everyone,
I have a question: is it possible that a derived class, inherite the values of the already initialized variables of the base class?
If yes, which is the way?
Thank you in advance for yours help.
A derived class inherits all members (variables and functions) from the base classes. The constructors of the base classes will run, and (hopefully) initialize the base member variables, before the constructor of the derived class runs.

You don't have to do anything special. This will always happen. By default the default constructor (the constructor that takes no arguments) of the base classes will be used. If you want to use another constructor you can specify in the constructor's initialization list.
1
2
3
4
5
Derived::Derived()
:	Base1(3), // Will use the Base1::Base1(int) constructor
	Base2() // Will use the default constructor of Base2. This is the default so this line is not needed.
{
}
Last edited on
I have an instance of the base class with its variables initialized. Then I recognize a certain type of derived class ( from the value of a variable) so I need to create an instance of the derived class with all the inherited variables initialized with the same values of the base class I had.
I hope that i've been more clear
Last edited on
Just create a constructor in the derived class which copy constructs the base class:
1
2
3
4
Derived(Base const &from)
: Base(from)
{
}
Last edited on
LB could you kindly explain better what you mean?
I gave actual code example, I'm not sure of any better way to explain. What in specific is confusing to you?
Why the reference has to be constant in the argument of the constructor in the derived class? Then, refering to your code I construe that the base class object "from" is passed to the base constructor: therefore has the base class got the copy constructor?
Thank you for your time.
Yes, the base class should have a protected copy constructor.
amidaraxar wrote:
Why the reference has to be constant in the argument of the constructor in the derived class?
Because it is good practice to have proper const-correctness.
Perfect, i've tried and it works. You've been really kind and helpful
Topic archived. No new replies allowed.