Suppose I have a class Alpha, which has two member variables, beta and gamma, which are objects of classes Beta and Gamma respectively:
class Alpha
{
public:
Beta beta_;
Gamma gamma_;
};
Now, the class Gamma itself has a member variable p_beta, which is a pointer to that same beta variable in Alpha. However, Gamma does not have a default constructor, but instead it must be constructed by passing p_beta:
class Gamma
{
public:
Beta* p_beta_;
Gamma(Beta* p_beta)
{
p_beta_ = p_beta;
}
};
So then, if I want to create an object alpha of class Alpha, I need to construct its member gamma_ in the initializer list of Alpha, given that Gamma does not have a default constructor:
class Alpha
{
public:
Beta beta_;
Gamma gamma_;
Alpha() : gamma_(&beta_){}
};
My question is: Will beta_ have already been created by the time gamma_ is constructed in this initializer list? I would have thought that the initializer list is called before creating any of the other member variables, in which case beta_ will not exist. If beta_ has not been created by then, then how can I pass a pointer to beta_ when constructing gamma_?