Accessing Nested Structures | Sololearn: Learn to code for FREE!


Accessing Nested Structures

Most of the examples on the internet show structures which are "nested" in the sense that struct A contains an object of struct B: struct B { int a = 3939; }; struct A { B obj; }; to which the member of struct B can be easily invoked by doing: A aObj; std::cout << aObj.obj.a; What if I have structs whose definitions are actually nested? struct A { struct B { int a = 3939; }; }; How would I access the member of struct B only by initializing an object of struct A? A aObj? std::cout << aObj....? Let's assume that changing the definition of the structures is not an option.

10/23/2018 10:28:11 AM

Hatsy Rei

10 Answers

New Answer


Without instancing the B struct within the A struct, I don't think there is a way, as B will never be an object. You would have to do something like: int main() { struct A { struct B { int a; }; B b; }; A aObj; aObj.b.a = 3; cout << aObj.b.a; return 0; }


That's correct jay . In fact, the struct A must have a member object of type B in order to be able to make an interface to outer world. BUT, in case of an unnamed struct/union as struct A { struct { int a; }; }; it is possible to to do something like A a; a.a = 3;


There is this: #include <iostream> using namespace std; int main() { struct A { struct B { int a = 3939; }; }; A::B bObj; cout << bObj.a; return 0; } But we aren't really instancing an object of A are we..


C++ Soldier (Babak) exactly 😉


jay "But we aren't really instancing an object of A are we.." Obviously not! You effectively dig through the scope of A, then accessed the B pretty much the same way that static members of a class can be accessed, and instantiating an object of type B outside the world of A. It merely transfers the inner instantiation in your first example, to the main which might be in part beneficial but your first attempt was more conventional and tend to be a kind of good practice.


Sonic Despite the question's intent, and also considering that the use case in which that snippet should be used is absent, it's always better to seek a good balance between feasibility of the implementation and ease of further maintenance. After all, for the most part, we write the code to change it later on according to the new demands and also new standards of the language.


jay Scope resolution operator. You beat me to it. :> I think it's the best we can get (without modifying the struct) to access the member within B, although we aren't really declaring an object of A, I guess. That said, C doesn't have scope resolution operator, (I'm not sure if it's even possible to nest structs like this in C) making it truly impossible to access members of B. The best course of action appears to be to never declare and implement the structures in my examples. :x


Hatsy Rei "I'm not sure if it's even possible[...]" C11 standard (n1517, §, p.26) says "63 levels of nested structure or union definitions in a single struct-declaration-list" is the translation limit.


I want an example of Ruby