I have the following code:
struct A {
protected:
A() {}
A* a;
};
struct B : A {
protected:
B() { b.a = &b; }
A b;
};
There are actually two separate problems here.
The first is that the line doesn't just do an assignment, but tries to initialize the base class (which works fine) and the member b
. To create the b
member it needs to construct it, and as a member it needs public
access to a constructor, which it doesn't have.
Then the assignment also is unable to access non-public member of b
because again, it's not of type B
but type A
instead.
Remember that protected
means you can access parts of A
through a B
object (or child) only.
In this case tell us your real problem and we can try to help solve it. Inheriting and composing from the same type is a design smell.