Comming from Java, I have difficulty with the code below.
In my understanding b is just declared on line 3 but not instantiated.
What would be the text book way of creating an instance of B in class A?
class A {
private:
B b;
public:
A() {
//instantiate b here?
}
};
Edit: What if B does not have a default constructor?
You could explicitly initialize
binA‘s constructor’s initialization list, for exampleHowever,
bwould get instantiated automatically anyway. The above makes sense if you need to build aBwith a non-default constructor, or ifBcannot be default initialized:It may be impossible to correctly initialize in the constructor’s initialization list, in which case an assignment can be done in the body of the constructor: