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.
{
}
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
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.