Nested structs and the usage of this

Suppose I have the following code:

1
2
3
4
5
6
7
8
struct N
{
int a;
struct NN
{
int b;
};
};


Is there a way to write a function in the inner struct that would return a pointer to the outer struct?
There are only minor differences between classes that are nested and those that aren't.
1
2
3
4
5
6
7
8
9
10
11
struct N
{
  int a;
  struct NN { N* get_n() { return new N; } }; // e.g.
};

int main() 
{
    N::NN nn; 
    delete nn.get_n();
}
Last edited on
You seem to create a new pointer to N whereas what I had in mind is returning a pointer to an outer structure for which memory was already allocated, something like this but it would need to point to N instead of NN.
The point is that N::NN has no association with N. These classes are completely independent - you might have written
1
2
struct N { int a; };
struct NN { int b; };

to the same effect.

If N contained a member variable of type N::NN instead, we could get closer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cassert>

struct N 
{
  struct NN 
  { 
    explicit NN(N* outer)
      : outer_(outer) 
    {}

    N* outer_;
  } nn{this};
};


int main()
{
  N n;
  N* pn = n.nn.outer_;
    
  assert(&n == pn);
}
Last edited on
I added an int, a, to the outer structure and wrote
cout << n.nn.outer_->a; in main. The code seems to do it's job but I don't understand what you've written. I have never used the explicit keyword before, not to mention writing such complex-looking statements. Could you please explain what is going on in your work?
It seems to be the case that for the purpose of displaying an outer variable lines 7, 8 and 9 are non-essential. Moreover, I tried declaring a variable in the inner structure before the pointer. That refuses to work, while declaring a variable after the pointer seems to be legal.

Also: is there a way to make nn a pointer?
Last edited on
yes, if you put an * on a NN variable you have a pointer.

usually 2 objects do not need to both know about each other in an inheritance chain but sometimes they do. when they do a pointer is an easy way to do it.

struct b; //forward declare lets struct A have a pointer to 'not yet seen' thing b.
struct a
{
int x;
b* bptr;
};

struct b
{
a avar;
}

b bvar;
… code to init everything, a's b pointer is set to parent somewhere in here.
bvar.avar.bptr-> ….

to make all that cleaner you need to construct the a inside of b using b's this pointer to populate a's b pointer. Then you probably only make instances of b class, and it has an a inside that is parental aware.

hopefully that made sense. Im half asleep atm.
Last edited on
It seems to be the case that for the purpose of displaying an outer variable lines 7, 8 and 9 are non-essential.
Yes, lines 7-9 are nonessential. Removing them is a compromise for brevity.

I have never used the explicit keyword before
explicit isn't important for the question. Once you look up what it does, just be aware that most single-argument constructors should be explicit. (If you're curious/have questions about it, open a new thread.)

I tried declaring a variable in the inner structure before the pointer. That refuses to work, while declaring a variable after the pointer seems to be legal.
Share the code that doesn't work?

Could you please explain what is going on in your work?
Each object of type N has a member variable of type N::NN named nn. When each object of type N is initialized, the nn's member variable outer_ is set to point to the newly-initialized N.

Also: is there a way to make nn a pointer?
This sounds like an XY problem ( http://xyproblem.info/ ). What issue do you hope to solve by doing this?
Last edited on
jonnin's answer is what I was looking for. Thank you!
Topic archived. No new replies allowed.