I have the following piece of code that is working but I don't understand why it works (inspired by a real life code base):
Base class definition:
class Pointers {
private:
int* Obj1;
double* Obj2;
public:
Pointers(int* Obj1_, double* Obj2_) : Obj1{Obj1_}, Obj2{Obj2_} {}
};
We now derive a class from our base class where we shadow the two pointers with an int
and a double
of the same name:
class Objects : public Pointers {
public:
int Obj1{69};
double Obj2{72};
Objects() : Pointers(&Obj1, &Obj2) {}
};
Within the constructor of Objects
we call the constructor of Pointers
and pass the adresses of Obj1
and Obj2
. This actually works: The (shadowed) pointers will point to Obj1 (69)
and Obj2 (72)
.
My question is: Why is this working? I thought that in the first step the base class members are constructed (which are the two pointers) and only after that the derived class members (the int
and double
with the same name) are constructed. How can we pass these objects adresses to the base class constructor in the first place?