+ 13

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.

23rd Oct 2018, 10:28 AM
Hatsy Rei
Hatsy Rei - avatar
10 ответов
+ 13
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; }
23rd Oct 2018, 10:55 AM
jay
jay - avatar
+ 16
I tried the following but it doesn't work! https://code.sololearn.com/c43G1aVpyS0l/?ref=app I also don't understand why the sizeof(struct A) is 1. Probably because there isn't an instance of the B structure within the A structure and just a spec/template. Edit: But I think jay has the right solution! Edit: C++ Soldier (Babak) Even if Jay's first attempt was better practice, I guess it's not allowed in the context of the question as modification of the structure is assumed to be not allowed? E.g. present in legacy, unmodifiable code perhaps?
24th Oct 2018, 4:58 AM
Sonic
Sonic - avatar
+ 9
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;
23rd Oct 2018, 12:23 PM
Babak
Babak - avatar
+ 7
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.. https://code.sololearn.com/cSHT4JNxljiO/?ref=app
24th Oct 2018, 5:03 AM
jay
jay - avatar
+ 6
C++ Soldier (Babak) exactly 😉
24th Oct 2018, 5:38 AM
jay
jay - avatar
+ 5
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.
24th Oct 2018, 5:36 AM
Babak
Babak - avatar
+ 5
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.
24th Oct 2018, 7:21 AM
Babak
Babak - avatar
+ 5
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
24th Oct 2018, 8:34 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Hatsy Rei "I'm not sure if it's even possible[...]" C11 standard (n1517, §5.2.4.1, p.26) says "63 levels of nested structure or union definitions in a single struct-declaration-list" is the translation limit.
24th Oct 2018, 8:58 AM
Babak
Babak - avatar
0
I want an example of Ruby
6th Jun 2021, 12:02 AM
حسين شكر فتحي المولى
حسين شكر فتحي المولى - avatar