class B {
// A a; // this is a data member of the class
// this holds a value of type A (it is a copy local to the object of type B)
// ie. an object of type B contains a sub-object of type A
A& a; // this is a reference member of the class
// this holds an alias (a reference) to the object of type A (constructor arg)
public:
B(A &a) : a(a) {}
const A &getA() const {
return a;
}
};
> So what does this constructor do? B(A &a) : a(a) {}
If a is a data member of the class, it copy initialises the data member.
(B::a becomes a copy of the object referred to by the constructor argument)
If a is a reference member of the class, it initialises the reference member.
(B::a refers to the same object that is referred to by the constructor argument)