You cannot. Even if the member was public
you cannot access a member of one class in the member of a second class unless you have an instance of that second class available.
Your code looks a little complicated because it has inheritance and what not. Let me try to explain by means of a simpler example:
struct F {
struct G {
int x;
};
int get();
};
int main() {
F::G g{42};
}
Now, similar question: How to return G::x
from F::get()
?
You can't.
What you can do is...
Add a G
member to F
and return the member of that instance:
struct F {
struct G {
int x;
};
G g;
int get() {
return g.x;
}
};
or pass a G
to get
:
struct F {
struct G {
int x;
};
int get(const G& g) {
return g.x;
}
};
Just because G
is defined inside F
does not make any instance of G
a member of a F
.
Actually also the nesting is not that relevant because with regards to accessing the member G::x
it is similar to, now with private
:
class G {
int x;
};
struct F {
int get() { /* how to access G::x here?!?! */ }
};
Now again two issues: First you need an instance of G
to access one of its members. Second, you cannot access it when it is private. As above, we pass an instance to get
and in addition add an accessor:
class G {
int x;
public:
G() : x(42) {}
int get() const { return x; }
};
struct F {
int get(const G& g) { return g; }
};
int main() {
G g;
F f;
int y = f.get(g);
}
Alternatively you can declare F
a friend of G
, then G::get
is not needed.
I suppose your code is a stripped down example of something more complex, because in the example code there is no reason to use F::get
to get the value of G::x
. Once you provided a way to access the member from outside the class, you can also access it directly in main
without using F::get
.