I have the following class called Tree, which has a member object of class Leaf. Leaf requires a parameter from Tree (height_) for its construction. I can write an intialize method for this. But do we know the order in which constructors are called, so that dependencies in the construction of member objects are met when the Tree class is constructed? In other words, when there is dependency in the instantiation of a member object, is an separate initialization method (for the member object) the only way to go? A minimal code below, I have put a question mark in the argument to the constructor of Leaf to indicate my question:
class Tree {
private:
float height_;
Leaf leaf_(?);
public:
explicit Leaf(const std::istream& input);
};
void Tree::Tree(const std::istream& input){
// read height_ from input
...
}
class Leaf {
private:
float height_fraction_;
public:
// height is required for construction of Leaf class
Leaf(const float& height);
};
void Leaf::Leaf(const float& height)
{
height_fraction_ = 0.5*height;
}