With the two classes below, I want to set b.i in the constructor of A. How do I do this?
I can't pass the value to the constructor of B, because I only get the value somewhere inside the constructor of A.
I tried making A::A()
a friend function of B, but that requires me to include a.hpp, which leads to circular inclusion.
The only solution I can come up with is changing the type of b
to B*
. Are there better ways?
A.hpp:
#include "B.hpp"
class A {
private:
B b;
public:
A();
}
B.hpp:
class B {
private:
int i;
public:
B()
}
A solution I found
A logical solution is to make A a friend of B, so it can access i.
I had tried this before, but then it didn't work. Apparently that was due to an other problem, or I did it incorrectly, because now it does work.